Class Inheritance in Java and use of Super keyword


What is inheritance and how do you use it in java, what are its basics and how to use super keyword while implementing inheritance

Class Inheritance

Before moving on to class inheritance in java let us review once again, what is Inheritance.

So as the general meaning of Inheritance suggest it is the phenomenon to get predefined properties from parent, which in OOP terms is parent class or base class and the child which takes in the property of that parent class or say base class or parent class is known as sub class. So, now let us see how Java takes on inheritance.

Let us consider the example of "Cars" the general properties of car is its transmission type, and topspeed. I am keeping this example short so that you could understand. And in the sub class I will classify them into various brand and car type. consider the following tree structure for the cars.


Cars(parent class/base class)
|_____Sports
|_____Sedan(sub class)
|_____Hatchback


class Cars{
    public String transmissionType;
    public int topSpeed;
}

public class Sedan extends Cars{
    public void printProperties(){
        System.out.println("transmissionType of Sedaan is :"+transmissionType);
        System.out.println("topSpeed of Sedaan is :"+topSpeed);
    }
    
    public static void main(String[] a){
        Sedan i = new Sedan();
        i.topSpeed = 200;
        i.transmissionType = "Manual";
        i.printProperties();
    }
}

the output of following program is like this

transmissionType of Sedaan is :Manual
topSpeed of Sedaan is :200

As you can see in this programming example that the sedan extends the cars, this means class Sedan is inheriting the properties of Cars class. As in the main method we see. I have created the object of class Sedan not Cars but I am using this object to set the properties of Car class. This is legal because Sedan is inheriting the class Cars and thus all the public properties and methods of class Cars will behave like properties of Sedan class too.

However you must note one thing that you can store refenence of sub-class object in parent type reference but you can never have parent type reference to subclass object. This way java provides you the power to organise things in the way you want. When we talk about Inheritance several factors comes into play. One to keep in mind is access modifiers of the properties.

You can never inherit any private properties of class. This will give error. and If you want to specifically refer to base class there is one thing you must remember is "super" keyword.

The super keyword refers to base class always, this can be used in two ways, one for refering fields and methods and one we have a super constructor which refers to the constructor of base class. for example consider follwoing program and please, pay special attention to the use of super.


class Cars{
    public String transmissionType = "manual";
    public int topSpeed = 100;
    
    Cars(){
        System.out.println("inside base class constructor");
    }
    
    public void printProperties(){
        System.out.println("transmissionType of Car is :"+transmissionType);
        System.out.println("topSpeed of Car is :"+topSpeed);
    }

}

public class Sedan extends Cars{
    public String transmissionType = "auto";
    public int topSpeed = 200;
    
    public Sedan(){
        super();
        System.out.println("Inside sub class Constructor");
    }
    
    public void printProperties(){
        super.printProperties();
        System.out.println("transmissionType of Sedaan is :"+transmissionType);
        System.out.println("topSpeed of Sedaan is :"+topSpeed);
    }
    
    public static void main(String[] a){
        Sedan i = new Sedan();
        i.printProperties();
    }
}

before discussing this program let me tell you the out put of this program

inside base class constructor
Inside sub class Constructor
transmissionType of Car is :manual
topSpeed of Car is :100
transmissionType of Sedaan is :auto
topSpeed of Sedaan is :200

This is same program with only few modification to show the working of super. If you have paid your attention to the program you'll see that Instead of setting the field values using the object I have pre defined them. And yet again in the base class as well as sub class the properties and method signature is exectly the same. So if now try to access using the object this will invoke the property of that class to which the object belong.

But if you want to tell the JVM that you want to call the method of base class you call the method using "super" as in super.printProperties(); // this will call the method from base class as out put displays.

and even if you want to work with with the constructors there is a super constructor available. which invoke tha super constructor. As you can see in the program when defining the Sedan constructor I have called super. However if even you won't call it the system calls it automatically. But in case you want to specifically mention it you must remember that you must mention it as first statement in the constructor.

You must be very clear in telling the compiler what you want to do. Java provide you lot of programming flexiblity along with the type safety of the program.