Monday, August 11, 2014

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");
    }
}

We define a child class that extends the above class as folows.
class B extends A{
    //Constructor of B
    B(){
        System.out.println("B's constructor");
    }
}

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();
}

The output of the above code is as shown below.
JAVA - INHERITANCE
A's constructor
B's constructor

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();
    }
}

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();
    }
}

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();
    }
}

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();
}

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


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

No comments :

Post a Comment