Constructor Chaining in Java

Constructor Chaining in Java:



Constructor chaining is the process of  calling the parent class constructor from the child class constructor.It can be achieved by using super() method.

//this is parent class.
class School
{
School()
{
System.out.println("Parent class Constructor");
}
}

//this is child class.
class Student extends School
{
Student()
{
super();
System.out.println("child class constructor");
}
}
public class Demo {
public static void main(String[] args) {
//creating child class object
Student student=new Student();

}
}

In this example School is the parent class and Student is the child class.When we create Student class object super() method get execute and take the control to the parent class constructor class then parent class constructor get executed . This type of chaining is called Constructor Chaining.




No comments

Powered by Blogger.