Different usage of super keyword in Java

Different usage of super keyword in Java :

super keyword is used to refer the parent class object. In this tutorial we will learn main three different usages of super :
1. Accessing values of parent class.
2. Accessing methods of parent class.
3. Invoking constructor of parent class.

Accessing values of a parent class :

Using super, we can access any non-private values of a super class. For example :

class Main {
    public static void main(String[] args) {
        Car car = new Car();
        car.printDetails();
    }
}

class FourWheeler {
    int wheelCount = 4;
}

class Car extends FourWheeler {
    void printDetails() {
        System.out.println("Name : Car");
        System.out.println("No. of wheels : " + super.wheelCount);
    }
}

It will print :

Name : Car
No. of wheels : 4

The value 4 is the value of wheelCount. We are using super keyword to read this value because FourWheeler class is the parent of Car class. We can also print this value by removing the super keyword.

2. Accessing methods of parent class :

I have made a little bit modification to the above program :

class New {
    public static void main(String[] args) {
        Car car = new Car();
        car.printDetails();
    }
}

class FourWheeler {
    private int wheelCount = 4;

    public void printWheelCount() {
        System.out.println("No. of wheels : " + wheelCount);
    }
}

class Car extends FourWheeler {
    void printDetails() {
        System.out.println("Name : Car");
        super.printWheelCount();
    }
}

It will print the same result as above.Only difference is that we are accessing printWheelCount() method of the super class to print the value of its private variable wheelCount. You can also call this method by removing the super keyword, i.e. using printWheelCount() instead of super.printWheelCount(). If we have one overriding method, then super is must to print the superclass method.For example :

class Main {
    public static void main(String[] args) {
        Car car = new Car();
        car.printDetails();
    }
}

class FourWheeler {
    private int wheelCount = 4;

    public void printWheelCount() {
        System.out.println("No. of wheels : " + wheelCount);
    }
}

class Car extends FourWheeler {
    public void printWheelCount() {
        System.out.println("Checking...");
    }

    void printDetails() {
        System.out.println("Name : Car");
        printWheelCount();
        super.printWheelCount();
    }
}

This time, without super call will invoke the same class method and with super will invoke the parent class method. Output is :

Name : Car
Checking...
No. of wheels : 4

3. Invoking constructor of the parent class :

Using super(), we can invoke constructor of the parent class. We can call the empty constructor or a constructor that takes parameter as argument. For example, in the program below, we are invoking the constructor of the parent class “FourWheeler” inside the constructor of the child class “Car” using super() :

class MainClass {
    public static void main(String[] args) {
        Car car = new Car();
    }
}

class FourWheeler {
    FourWheeler(){
        System.out.println("Inside constructor of fourwheeler");
    }
}

class Car extends FourWheeler {
    Car(){
        super();
        System.out.println("Inside constructor of car");
    }
}

It will print the following output :

Inside constructor of fourwheeler
Inside constructor of car

That means, the constructor of the parent class is called before the child class. Similarly, we can also call any constructor with a parameter like :

class MainClass {
    public static void main(String[] args) {
        Car car = new Car();
    }
}

class FourWheeler {
    FourWheeler(){
        System.out.println("Inside constructor of fourwheeler");
    }
    FourWheeler(String param){
        System.out.println("Inside constructor of fourwheeler with param : "+param);
    }
}

class Car extends FourWheeler {
    Car(){
        super("car");
        System.out.println("Inside constructor of car");
    }
}

It will print :

Inside constructor of fourwheeler with param : car
Inside constructor of car

Leave a Reply