We will look at constructors and what gets called first and how the flow is when it comes to inheritance.  If there are cases that I have missed out, please feel free to comment so that I could add them for everyone else who can benefit. 🙂

Case 1:
Let’s take three classes A, B and C. B extends A and C extends B

A.java
public class A {
    public A() {
        System.out.println("Constructor A");
    }

}

B.java
public class B extends A{
    public B() {
        System.out.println("Constructor B");
    }
    public B(String value) {
        System.out.println("Constructor B parameterized");
    }
}

C.java
public class C extends B{
    public C() {
        System.out.println("Constructor C");
    }
    public static void main(String[] args) {
        new C();
    }
}

When the following gets executed, we have the following output. In inheritance, the base class constructor will always get called.

Output:
Constructor A
Constructor B
Constructor C

Case 2:
We now modify C.java to look like below:

public class C extends B{
    public C() {
        super("i am called from C constructor");
        System.out.println("Constructor C");
}

    public static void main(String[] args) {
        new C();
    }
}

The output would now be:
Constructor A
Constructor B parameterized
Constructor C

We do not have the default constructor of class B getting called since we have called in super with a parameter, so the super constructor would be called, which is B’s constructor but the one with a parameter.

Case 3:
We modify the same class C again and make it look like below:

public class C extends B{
    public C() {
        super("i am called from C constructor");
        new B();
        System.out.println("Constructor C");
    }
    public static void main(String[] args) {
        new C();
    }
}

The output would now be:
Constructor A
Constructor B parameterized (since we are calling super with a parameter)
Constructor A
Constructor B
Constructor C

So then why do we have the constructor A line printed twice? Because in the first case, when C is getting created, the base constructor would be called which would be B but then since B is also extending a class, its base would be called, so we have A getting called.

 

constructors

Why then do we have the base constructor getting called? This is so as to initialize all the required properties (variables) of the base classes to make them available for the child class.

*Just for information, we can write:

A a = new C(); or B b = new C();

But writing B b = new A();  or any such line would cause a compile time error

I will take an example of the Animal and Dog example to explain this. We can have a base class Animal and a child class Dog.

Thus when we write:

Dog dog = new Animal();

We denote or rather try to say in layman’s language that an animal is a dog, or probably all animals are dogs, which is not true is the animal is say may be a cat, because a cat cannot be a dog. And also this assignment means that the base class has the same capabilities as the child class which is usually not the case in inheritance since the child class always has more functionalities than the base class.

Whilst, the statement

Animal animal = new Dog();

holds because a dog is an animal, similarly if we wish to say a cat is an animal we can easily say so.

 

Advertisements