What is Constructor chaining?
Executing current class constructor and super class constructor up to the object class no argument constructor is called the constructor chaining.
Hard and Fast rule for constructor chaining.
Rule no 1:-If no constructor is defined, compiler will define it for you, which is the default constructor, and the first call in the default constructor is the call to the super ().
Rule no 2:-If you have your own constructor, then compile will see if ‘this()’ is being called or not,if this() is not called, it will insert the super() as the first call.
These 2 two points are sufficient to understand constructor chaining. :-) if you understood these rules you have win the half battle.
Executing current class constructor and super class constructor up to the object class no argument constructor is called the constructor chaining.
Hard and Fast rule for constructor chaining.
Rule no 1:-If no constructor is defined, compiler will define it for you, which is the default constructor, and the first call in the default constructor is the call to the super ().
Rule no 2:-If you have your own constructor, then compile will see if ‘this()’ is being called or not,if this() is not called, it will insert the super() as the first call.
These 2 two points are sufficient to understand constructor chaining. :-) if you understood these rules you have win the half battle.
Consider the following example.
class Person {
int id = 10;
String name;
Person(int i) {
System.out.println("person"+i);
}
}
class Employee extends Person{
Employee(){
}
}
public class Constructer {
public static void main(String[] args) {
Person p = new Person(10);
}
}
What do you think, will it compile successfully.Lets apply the two rules.
Start from the Employee class.
Observation 1.Employee class does have a no-arg constructor defined,the compiler will next see if this constructor has a this() call, no then will put a super();
Employee()
{
super();
}
Observation 2.because of super() call, it will go to the Person class and looks for the no argument constructor in Person class but the person class does not have a no-arg constructor in it,as a result gives a compile time error.
Below code will compile successfully.
class Person {
int id = 10;
String name;
Person(){
}
Person(int i){
System.out.println("person"+i);
}
}
class Employee extends Person{
Employee(){
}
}
public class Constructor {
public static void main(String[] args) {
Person p = new Person(10);
}
}
******************************************************************************************************
Example 2.
class Person {
int id = 10;
String name;
Person(int i){
System.out.println("person"+i);
}
}
class Employee extends Person{
int i = 200;
double salary;
Employee(int i){
super(i);
System.out.println("Employee");
}
}
public class Test {
public static void main(String[] args) {
Person p = new Employee(10);
}
}
Observation 1.Employee Class has a constructor which takes int as an argument ,compiler checks if it has call to this() or super(),and finds that super(i) is present.
Employee(int i){
super(i);
System.out.println("Employee");
}
Employee(int i){
super(i);
System.out.println("Employee");
}
Observation 2.because of super(i), it goes to its parent class which is Person class and search for a constructor which takes an int as an argument,an find one.
Person(int i){
System.out.println("person"+i);
}
Again compiler check if in the Person(int i) constructor, this() is present says NO,so compiler then inserts the super(),because of which it will go to the object class.
Person(int i){
System.out.println("person"+i);
}
Again compiler check if in the Person(int i) constructor, this() is present says NO,so compiler then inserts the super(),because of which it will go to the object class.
Result complies successfully.
******************************************************************************************************
Example 3.
class Person
{
int id = 10;
String name;
Person(int i){
System.out.println("person with arg constructor");
}
{
System.out.println("person initilization block");
}
Person(){
this(10);
System.out.println("person no arg constructor");
}
}
class Employee extends Person{
Employee(int i){
System.out.println("Employee with arg constructor");
}
{
System.out.println("Employee initilization block");
}
Employee(){
this(10);
System.out.println("Employee no arg constructor");
}
}
public class Test {
public static void main(String[] args) {
new Person();
new Employee();
}
}
******************************************************************************************************
Example 3.
class Person
{
int id = 10;
String name;
Person(int i){
System.out.println("person with arg constructor");
}
{
System.out.println("person initilization block");
}
Person(){
this(10);
System.out.println("person no arg constructor");
}
}
class Employee extends Person{
Employee(int i){
System.out.println("Employee with arg constructor");
}
{
System.out.println("Employee initilization block");
}
Employee(){
this(10);
System.out.println("Employee no arg constructor");
}
}
public class Test {
public static void main(String[] args) {
new Person();
new Employee();
}
}
Note:-In constructor chaining, initialization block executes only when the control returns back from the super class constructor.
Observation 1." new Person();":-Call the Person Class no arg constructor,sees if it has this() or super() call.
Observation 2. finds this(10),looks in the person class for a constructor which accepts int as a argument, and find,
Person(int i){
System.out.println("person with arg constructor");
}
again look for this() or super(), but its not there ,so it insert super(),as follows
Person(int i){
super();
System.out.println("person with arg constructor");
}
goes to the object class,since Person class by default extends Object class,when control return from the object class to the Peron class it will execute the initialization block,then the Person(int i) constructor then Person() constructor .
The output will be as follows "person initialization block"---->"person with arg constructor"--->"person with arg constructor"
Observation 3
new Employee() calls the Employee class no argument constructor.
Employee(){
this(10);
System.out.println("Employee no arg constructor");
}
Encounter "this(10)",search for the constructor in the Employee Class which takes int as an argument.
And find the following code.
Employee(int i){
System.out.println("Employee with arg constructor");
}
sees if "this()" is there,say NO so compiler will insert the "super()". OK,on seeing super() compiler looks for the parent class of the Employee, which has a no argument constructor, as a result goes to the following constructor.
Person(){
this(10);
System.out.println("person no arg constructor");
}
Compiler sees the "this(10)" as a result does not insert "super()". super() will come only when this() is not there. this(10) will make a call to the constructor in Person class which takes int as an argument,and find the following snippet.
Person(int i){
System.out.println("person with arg constructor");
}
Again the compiler will see if this() is present in the constructor,say no so insert super(),super() will call the object class constructor, on returning from the object class it will execute the initialization block code,then the Person(int i) constructor and the Person() constructor,then Employee initilization block, then Employee(int i) code and finally Employee() code .
******************************************************************************************************
Understanding why java does not support multiple inheritance.
Observation 2. finds this(10),looks in the person class for a constructor which accepts int as a argument, and find,
Person(int i){
System.out.println("person with arg constructor");
}
again look for this() or super(), but its not there ,so it insert super(),as follows
Person(int i){
super();
System.out.println("person with arg constructor");
}
goes to the object class,since Person class by default extends Object class,when control return from the object class to the Peron class it will execute the initialization block,then the Person(int i) constructor then Person() constructor .
The output will be as follows "person initialization block"---->"person with arg constructor"--->"person with arg constructor"
Observation 3
new Employee() calls the Employee class no argument constructor.
Employee(){
this(10);
System.out.println("Employee no arg constructor");
}
Encounter "this(10)",search for the constructor in the Employee Class which takes int as an argument.
And find the following code.
Employee(int i){
System.out.println("Employee with arg constructor");
}
sees if "this()" is there,say NO so compiler will insert the "super()". OK,on seeing super() compiler looks for the parent class of the Employee, which has a no argument constructor, as a result goes to the following constructor.
Person(){
this(10);
System.out.println("person no arg constructor");
}
Compiler sees the "this(10)" as a result does not insert "super()". super() will come only when this() is not there. this(10) will make a call to the constructor in Person class which takes int as an argument,and find the following snippet.
Person(int i){
System.out.println("person with arg constructor");
}
Again the compiler will see if this() is present in the constructor,say no so insert super(),super() will call the object class constructor, on returning from the object class it will execute the initialization block code,then the Person(int i) constructor and the Person() constructor,then Employee initilization block, then Employee(int i) code and finally Employee() code .
******************************************************************************************************
Understanding why java does not support multiple inheritance.
Lets see the code first.
class Employee {
}
class Student {
}
class Person extends Student,Person{
Person()
{
super(); //ambiguity arises where to go Student or Employee
}
}
class Employee {
}
class Student {
}
class Person extends Student,Person{
Person()
{
super(); //ambiguity arises where to go Student or Employee
}
}
******************************************************************************************************