Java Inheritance – Super keyword

Inheritance is an important principle in object oriented programming. It is a mechanism in which one object acquires the property of another. The object which gives the properties is called as super/base/parent class and the one that receives the property is called as the sub/derived/child class. The child class inherits all fields and methods of the parent class extending it to add its own functionality by adding fields and methods.
One of the major advantages of Inheritance is reusability. The derived class gets the properties of the base class, thereby the code within the base is reused in the derived class, leading to faster development cycle.

This post looks at the effect inheritance has on the constructor and the methods, and particularly the role of super keyword in inheritance.

Incase of inheritance when an object of derived class is created the compiler first calls the base class constructor and then the derived class constructor. In case of multilevel inheritance the constructor would be called according to the order of inheritance.

Incase we have a method belonging to the base and derived class that have the same signatures, and if this common method is called, the compiler overrides the method belonging to the base class and calls the method belonging to the derived class.

Lets define a simple class A as shown in the snippet below. This is going to be our parent class.

class A {
//Constructor of A
A() {
System.out.println("A's constructor");
}
public void method() {
System.out.println("A's get method");
}
}
view raw ji1.java hosted with ❤ by GitHub

We define a child class that extends the above class as follows.

class B extends A{
//Constructor of B
B(){
System.out.println("B's constructor");
}
}
view raw ji2.java hosted with ❤ by GitHub

Here is a sample test main method for testing the inheritance.

public static void main(String[] args) {
System.out.println("JAVA – INHERITANCE");
B objectB = new B();
}
view raw ji3.java hosted with ❤ by GitHub

The output of the above code is as shown below.

JAVA – INHERITANCE
A's constructor
B's constructor
view raw ji4.txt hosted with ❤ by GitHub

As we see, the base class constructor is called implicitly. We can also call the base class constructor explicitly by using super(). However, the super call should be the first statement in the derived class constructor. The following code snippet will give a compiler error.

class B extends A{
//Constructor of B
B(){
System.out.println("B's constructor"); //This is not allowed
super();
}
}
view raw jp5.java hosted with ❤ by GitHub

The super() call has to be the first line in the constructor, as its important to initialize the base class before the derived class is initialized.
The correct way of writing the above code is as follows:

class B extends A{
//Constructor of B
B(){
super();
}
}
view raw ji6.java hosted with ❤ by GitHub

However, a base class method can be called from any line of the method in the derived class using the keyword super. This is shown in the snippet below.

class B extends A{
//Constructor of B
B(){
super();
}
@Override
public void method(){
System.out.println("B's get method");
super.method();
}
}
view raw ji7.java hosted with ❤ by GitHub

Here is the sample main function where we call the derived class method..

public static void main(String[] args){
System.out.println("JAVA – INHERITANCE");
B objectB = new B();
objectB.method();
}
view raw ji8.java hosted with ❤ by GitHub

The output of the above code is as shown below.

JAVA – INHERITANCE
A's constructor
B's constructor
B's get method
A's get method
view raw ji9.txt hosted with ❤ by GitHub

Thus we have seen in this post that the parent class constructor is executed before the child class constructor. If the base class contains a constructor with one or more arguments, then it is mandatory for the derived class to have a constructor and pass the arguments to the base class constructor

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s