Constructor Calling

When a class inherit another class then child class depends on parent class data. It make sense that before initialising child class object, parent class object need to be initialised. In java whenever child class constructor is called, it make a call to parent class constructor.

Example:


 class one 
{
one()
{
System.out.println("one class constructor");
}
}
class two extends one
{
two()
{
System.out.println("two class constructor");
}
public static void main(String arg[])
{
new one();
}
}
/*Output:
one class constructor
two class constructor
*/

Important ! Constructor are called in the sequence of thier derivation or inheritance

Example :


 class vehicle
{
vehicle()
{
System.out.println("Vehicle class");
}
}
class two_wheeler extends vehicle
{
two_wheeler()
{
System.out.println("two_wheeler class");
}
}
class bycycle extends two_wheeler
{
bycycle()
{
System.out.println("bycycle class");
}
public static void main(String arg[])
{
new bycycle();
}
}
/*Output:
vehicle class
two_wheeler class
bycycle class
*/


Calling parameterised constructor of parent class

Child class constructor always call parent class default constructor.

Example :


 class A
{
int a;
A()
{
System.out.println("A's Default constructor ");
}
A(int x)
{a=x;
System.out.println("A's parameterised constructor ");
}
}
class B extends A
{ int b;
B()
{
System.out.println("B's Default constructor");
}
B(int y)
{
b=y;
System.out.println("B's parameterised constructor ");
}
public static void main(String arg[])
{
new B();
new B(12)
}
}
/*Output:
A's Default constructor
B's Default constructor
A's Default constructor
B's parameterised constructor
*/

Above programs shows that even when parent class has multiple constructor only default constructor get called on initialisation of child class object.



Super Keyword

super keyword is used to call constructor of parent class. In child class super keyword used to refer parent class.


Example :


 class A
{
int a;
A()
{
System.out.println("A's Default constructor ");
}
A(int x)
{a=x;
System.out.println("A's parameterised constructor ");
}
}
class B extends A
{ int b;
B()
{
System.out.println("B's Default constructor");
}
B(int y)
{
super(y); // It will call parent class parameterised constructor
b=y;
System.out.println("B's parameterised constructor ");
}
public static void main(String arg[])
{
new B();
new B(12)
}
}
/*Output:
A's Default constructor
B's Default constructor
A's parameterised constructor
B's parameterised constructor
*/

super keyword also used in case when parent class and child class both have same name variable. super variable can be used to access parent class variable.


Example :


 class A
{
int a;
A()
{
a=10;
}
}
class B extends A
{ int a;
B()
{
a=20;
}
void print()
{
System.out.println(a);
System.out.println(a);
}
public static void main(String arg[])
{
B obj=new B();
obj.print();
}
}
/*Output:
20
20
*/

Above program having two classes A and B. Class A has variable a and class B inheriting class A therefore class B having two variable both with same name a and a. In child class in print method printing value of a, which always print value of B's variable a. It won't print A's variable value.


To access parent class variable with same name super variable can be used.

Example :


 class A
{
int a;
A()
{
a=10;
}
}
class B extends A
{ int a;
B()
{
a=20;
}
void print()
{
System.out.println(a);
System.out.println(super.a);
}
public static void main(String arg[])
{
B obj=new B();
obj.print();
}
}
/*Output:
20
10
*/