22 09 10 constructor
TRANSCRIPT
-
8/8/2019 22 09 10 Constructor
1/15
Constructors
When you create a new instance (a new
object) of a class using the new keyword,
a constructorfor that class is called. Constructors are similar to methods, but
with some important differences.
-
8/8/2019 22 09 10 Constructor
2/15
Constructor name is class name. Aconstructors must have the same name as
the class its in. Default constructor. If you don't define a
constructor for a class, a defaultparameterless constructoris automaticallycreated by the compiler. The defaultconstructor calls the default parentconstructor (super()) and initializes allinstance variables to default value (zerofor numeric types, null for objectreferences, and false for booleans).
-
8/8/2019 22 09 10 Constructor
3/15
-
8/8/2019 22 09 10 Constructor
4/15
this(...) - Calls another constructor in same
class. Often a constructor with few parameters
will call a constructor with more parameters,giving default values for the missing parameters.
Use this to call other constructors in the same
class.
super(...). Use superto call a constructor in aparent class. Calling the constructor for the
superclass must be the first statementin the
body of a constructor. If you are satisfied with
the default constructor in the superclass, there isno need to make a call to it because it will be
supplied automatically.
-
8/8/2019 22 09 10 Constructor
5/15
Box class with Constructor
class Box
{double width;double height;double depth;
Box() {System.out.println("Costructing a Box");width=3;heigth=5;depth=6;
}
double volume(){return width*height*depth;
}}
-
8/8/2019 22 09 10 Constructor
6/15
The usage of the Constructor
class BoxTest {public static void main(String arg[]) {Box b1=new Box();Box b2=new Box();double vol;
vol=b1.volume();System.out.println("Volume is:"+vol);
vol=b2.volume();
System.out.println("Volume is:"+vol);}}
-
8/8/2019 22 09 10 Constructor
7/15
parameterized Constructorclass Box{double width;double height;double depth;
Box(double w,double h,double d) {width=w;heigth=h;depth=d;}
double volume(){return width*height*depth;
}}
-
8/8/2019 22 09 10 Constructor
8/15
How to use parameterized Constructor
class BoxTest {public static void main(String arg[]) {Box b1=new Box(2,5,9);Box b2=new Box(7,10,12);double vol;
vol=b1.volume();System.out.println("Volume is:"+vol);
vol=b2.volume();System.out.println("Volume is:"+vol);}}
-
8/8/2019 22 09 10 Constructor
9/15
Sub classing and Inheritance
Class Parent
{ }
Class Child extends Parent
{ }
-
8/8/2019 22 09 10 Constructor
10/15
Method Overloading
class Parent
{void calc(){System.out.println("This is
Addition");}
int calc(int x){return x/2;}
Void calc(int x, int y) {System.out.println("This is
multiplying");}
}
-
8/8/2019 22 09 10 Constructor
11/15
Method Overriding
class Parent{
void calc(){System.out.println("This is parent Addition");
}}
class Child extends Parent {void calc(){System.out.println("This is child Addition");
}public static void main(String a[]){
Parent p=new Parent();Child c=new Child();p.calc();c.calc();
}}
-
8/8/2019 22 09 10 Constructor
12/15
Overloading Constructor
class Parent{
parent(){System.out.println("This is default
Costructor");}parent(int x){
System.out.println("Costructoroverloaded"+x);}}
class Child extends Parent {
public static void main(String a[]){Parent p1=new Parent();Parent p1=new Parent(10);Child c1=new Child();
}}
-
8/8/2019 22 09 10 Constructor
13/15
How to invoke parent defaultConstructor by child class
class Parent
{parent(){System.out.println("This is parent Costructor");
}
parent(int x){
System.out.println("parent Costructor with args"+x);}
class Child extends Parent {
Child(int y){
System.out.println("Child Costructor with args"+y);}
public static void main(String a[]){Child c1=new Child(10);
}}
-
8/8/2019 22 09 10 Constructor
14/15
-
8/8/2019 22 09 10 Constructor
15/15
How to use this() to invoke Constructor
class Parent
{parent(){System.out.println("This is parent Costructor");
}
parent(int x){this();System.out.println("parent Costructor with args"+x);
}
class Child extends Parent {
public static void main(String a[]){
Parent p1=new Parent(10);}
}