Constructors, Overloading and Static Members 1
Constructors  Java allows objects to initialize themselves when they are created through the use of a constructor.  Constructors play the role of initializing objects  Constructors are a special kind of methods that are invoked using the new operator when an object is created.
Constructors, cont.  Constructors have following properties:  Constructors must have the same name as the class itself.  Constructors do not have a return type— not even void.  The implicit return type is class itself  A constructor with no parameters is referred to as a default constructor and constructor with parameters is called as parameterized constructor 3
Program for constructor class Box { double width; double height; double depth; // This is the constructor for Box. Box() { System.out.println("Constructing Box"); width = 10; height = 10; depth = 10; } // compute and return volume double volume() { return width * height * depth; } } class BoxDemo1 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(); //constructor invoked Box mybox2 = new Box(); //constructor invoked double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } 4
Parameterized Constructors class Box { double width; double height; double depth; // This is the constructor for Box. Box(double w, double h, double d) { width = w; height = h; depth = d; } // compute and return volume double volume() { return width * height * depth; } } class BoxDemo2 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(3, 6, 9); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } 5
Exercise  WAP to create class rectangle and calculate area and perimeter of it by using constructor.  WAP to create class Employee. Add methods to read and display employee information by using constructor such as id and salary. 6
Method Overloading  Two or more methods within the same class that have the same name but methods must differ in the type and/or number of their parameters.  Method overloading is one of the ways that Java implements polymorphism  When an overloaded method is invoked, Java uses the type and/or number of arguments to determine which version of the overloaded method to actually call. 7
Example class OverloadDemo { void test() { System.out.println("No parameters"); } void test(int a) { System.out.println("a: " + a); } void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } double test(double a) { System.out.println("double a: " + a); return a*a; } } class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; // call all versions of test() ob.test(); ob.test(10); ob.test(10, 20); result = ob.test(123.2); System.out.println("Result of ob.test(123.2): " + result); } } 8
Constructor Overloading  Similar to method overloading we can also overload constructor .  Overloaded constructors have the same name but must differ in the type and/or number of their parameters. 9
Example class Box { double width; double height; double depth; // constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; } // constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box } // constructor used when cube is created Box(double len) { width = height = depth = len; } // compute and return volume double volume() { return width * height * depth; } class OverloadCons { public static void main(String args[]) { // create boxes using the various constructors Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(); Box mycube = new Box(7); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); // get volume of cube vol = mycube.volume(); System.out.println("Volume of mycube is " + vol); } } 10
Exercise  WAP to overload add method to perform addition of two integers, addition of three integers and addition of two float numbers.  WAP to create overloaded constructor to calculate area of triangle ,rectangle and circle. 11
Using Objects as Parameters class Test { int a, b; Test(int i, int j) { a = i; b = j; } void add(Test o) { if(o.a == a && o.b == b) System.out.println(" Equal”); else System.out.println(" Not Equal”); } } class PassOb { public static void main(String args[]) { Test ob1 = new Test(100, 22); Test ob2 = new Test(100, 22); Test ob3 = new Test(-1, -1); ob1.equals(ob2)); ob1.equals(ob3)); } } 12
Returning Object class Test { int a ,b; Test() { a = 0; b=0; } Test(int i ,int j) { a = i; b=j; } Test add() { Test temp = new Test(); temp.a=a+10; temp.b=b+10; return temp; }} class RetOb { public static void main(String args[]) { Test ob1 = new Test(2 ,4); Test ob2; ob2 = ob1.add(); System.out.println("ob1.a: " + ob1.a); System.out.println("ob1.b: " + ob1.b); System.out.println("After Addition :"); System.out.println("ob2.a: " + ob2.a); System.out.println("ob2.b: " + ob2.b); } } 13
static Members  Normally a class member must be accessed only with an object.  When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object.  There can be a static method, static variable and a static block  When objects of class are declared, no copy of a static variable is made.  Instead, all instances of the class share the same static variable.  Static methods can only call other static methods  Static methods must only access static data  Static block gets executed exactly once, when the class is first loaded. 14
class UseStatic { static int a = 3; static int b; static void meth(int x) { System.out.println("x = " + x); System.out.println("a = " + a); System.out.println("b = " + b); } static { System.out.println("Static block initialized."); b = a * 4; } public static void main(String args[]) { meth(42); }} OUTPUT Static block initialized. x = 42 a = 3 b = 12 15
 Outside of the class in which they are defined, static methods and variables can be used independently of any object.  To call a static method and variable from outside its class, class name is used . General form: classname.method( ); Classname.variable1; 16
class StaticDemo { static int a = 42; static int b = 99; static void callme() { System.out.println("a = " + a); } } class StaticByName { public static void main(String args[]) { StaticDemo.callme(); System.out.println("b = " + StaticDemo.b); } }  output : a = 42 b = 99 17

Session 3 Constructors, Types, Overloading, Static MethodsNotes.pdf

  • 1.
  • 2.
    Constructors  Java allowsobjects to initialize themselves when they are created through the use of a constructor.  Constructors play the role of initializing objects  Constructors are a special kind of methods that are invoked using the new operator when an object is created.
  • 3.
    Constructors, cont.  Constructorshave following properties:  Constructors must have the same name as the class itself.  Constructors do not have a return type— not even void.  The implicit return type is class itself  A constructor with no parameters is referred to as a default constructor and constructor with parameters is called as parameterized constructor 3
  • 4.
    Program for constructor classBox { double width; double height; double depth; // This is the constructor for Box. Box() { System.out.println("Constructing Box"); width = 10; height = 10; depth = 10; } // compute and return volume double volume() { return width * height * depth; } } class BoxDemo1 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(); //constructor invoked Box mybox2 = new Box(); //constructor invoked double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } 4
  • 5.
    Parameterized Constructors class Box{ double width; double height; double depth; // This is the constructor for Box. Box(double w, double h, double d) { width = w; height = h; depth = d; } // compute and return volume double volume() { return width * height * depth; } } class BoxDemo2 { public static void main(String args[]) { // declare, allocate, and initialize Box objects Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(3, 6, 9); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume is " + vol); } } 5
  • 6.
    Exercise  WAP tocreate class rectangle and calculate area and perimeter of it by using constructor.  WAP to create class Employee. Add methods to read and display employee information by using constructor such as id and salary. 6
  • 7.
    Method Overloading  Twoor more methods within the same class that have the same name but methods must differ in the type and/or number of their parameters.  Method overloading is one of the ways that Java implements polymorphism  When an overloaded method is invoked, Java uses the type and/or number of arguments to determine which version of the overloaded method to actually call. 7
  • 8.
    Example class OverloadDemo { voidtest() { System.out.println("No parameters"); } void test(int a) { System.out.println("a: " + a); } void test(int a, int b) { System.out.println("a and b: " + a + " " + b); } double test(double a) { System.out.println("double a: " + a); return a*a; } } class Overload { public static void main(String args[]) { OverloadDemo ob = new OverloadDemo(); double result; // call all versions of test() ob.test(); ob.test(10); ob.test(10, 20); result = ob.test(123.2); System.out.println("Result of ob.test(123.2): " + result); } } 8
  • 9.
    Constructor Overloading  Similarto method overloading we can also overload constructor .  Overloaded constructors have the same name but must differ in the type and/or number of their parameters. 9
  • 10.
    Example class Box { doublewidth; double height; double depth; // constructor used when all dimensions specified Box(double w, double h, double d) { width = w; height = h; depth = d; } // constructor used when no dimensions specified Box() { width = -1; // use -1 to indicate height = -1; // an uninitialized depth = -1; // box } // constructor used when cube is created Box(double len) { width = height = depth = len; } // compute and return volume double volume() { return width * height * depth; } class OverloadCons { public static void main(String args[]) { // create boxes using the various constructors Box mybox1 = new Box(10, 20, 15); Box mybox2 = new Box(); Box mycube = new Box(7); double vol; // get volume of first box vol = mybox1.volume(); System.out.println("Volume of mybox1 is " + vol); // get volume of second box vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); // get volume of cube vol = mycube.volume(); System.out.println("Volume of mycube is " + vol); } } 10
  • 11.
    Exercise  WAP tooverload add method to perform addition of two integers, addition of three integers and addition of two float numbers.  WAP to create overloaded constructor to calculate area of triangle ,rectangle and circle. 11
  • 12.
    Using Objects asParameters class Test { int a, b; Test(int i, int j) { a = i; b = j; } void add(Test o) { if(o.a == a && o.b == b) System.out.println(" Equal”); else System.out.println(" Not Equal”); } } class PassOb { public static void main(String args[]) { Test ob1 = new Test(100, 22); Test ob2 = new Test(100, 22); Test ob3 = new Test(-1, -1); ob1.equals(ob2)); ob1.equals(ob3)); } } 12
  • 13.
    Returning Object class Test{ int a ,b; Test() { a = 0; b=0; } Test(int i ,int j) { a = i; b=j; } Test add() { Test temp = new Test(); temp.a=a+10; temp.b=b+10; return temp; }} class RetOb { public static void main(String args[]) { Test ob1 = new Test(2 ,4); Test ob2; ob2 = ob1.add(); System.out.println("ob1.a: " + ob1.a); System.out.println("ob1.b: " + ob1.b); System.out.println("After Addition :"); System.out.println("ob2.a: " + ob2.a); System.out.println("ob2.b: " + ob2.b); } } 13
  • 14.
    static Members  Normallya class member must be accessed only with an object.  When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object.  There can be a static method, static variable and a static block  When objects of class are declared, no copy of a static variable is made.  Instead, all instances of the class share the same static variable.  Static methods can only call other static methods  Static methods must only access static data  Static block gets executed exactly once, when the class is first loaded. 14
  • 15.
    class UseStatic { staticint a = 3; static int b; static void meth(int x) { System.out.println("x = " + x); System.out.println("a = " + a); System.out.println("b = " + b); } static { System.out.println("Static block initialized."); b = a * 4; } public static void main(String args[]) { meth(42); }} OUTPUT Static block initialized. x = 42 a = 3 b = 12 15
  • 16.
     Outside ofthe class in which they are defined, static methods and variables can be used independently of any object.  To call a static method and variable from outside its class, class name is used . General form: classname.method( ); Classname.variable1; 16
  • 17.
    class StaticDemo { staticint a = 42; static int b = 99; static void callme() { System.out.println("a = " + a); } } class StaticByName { public static void main(String args[]) { StaticDemo.callme(); System.out.println("b = " + StaticDemo.b); } }  output : a = 42 b = 99 17