As we have seen in the previous tutorial that member variables and methods of SuperClass can only be accessed in subClass if they are declared as public. There are multiple uses of super keyword in Java, Lets look at them one by one.

super() to call superClass constructor

Let's modify out previous example of Inheritance and consider we have a Box class in which member variables are defined private.

public class Box {

    private int height;
    private int width;
    private int length;

    public Box() {
        this.height = -1;
        this.width = -1;
        this.length = -1;
    }

    public Box(int height, int width, int length) {
        this.height = height;
        this.width = width;
        this.length = length;
    }

    public double calculateVolume() {
        return height * width * length;
    }

}

Now we cannot access height, width and length parameter of Box class directly in the sub class. Thus to initialise their values we can call the constructor of super class by using super() method.

public class BoxWeight extends Box {

    int weight;

    public BoxWeight(int height, int width, int length, int weight) {
        super(height, width, length);
        this.weight = weight;
    }
    
    public void printWeight(){
        System.out.println("Weight is "+weight);
    }

}

super(height,width,length) will call the constructor of super class i.e. Box(height, width, length).
Note that call to a super in the constructor should be the first statement. Otherwise you will receive a compilation error of "Constructor call must be the first statement in a
constructor".

Use of Super to refer immediate super class instance variables

super keyword can also be used to refer to the instance variables of immediate parent Class. consider the example below.

public class Box {

    public int height;
    public int width;
    public int length;

    public Box() {
        this.height = -1;
        this.width = -1;
        this.length = -1;
    }

    public Box(int height, int width, int length) {
        this.height = height;
        this.width = width;
        this.length = length;
    }

    public double calculateVolume() {
        return height * width * length;
    }

}
package info.FiveBalloons.Examples;

public class BoxWeight extends Box {

    int weight;

    public BoxWeight(int height, int width, int length, int weight) {
        super(height, width, length);
        this.weight = weight;
    }

    public void printWeight() {
        System.out.println("Weight is " + weight);
    }

    public void getVariables() {
        System.out.println("Height is " + super.height + " Width is "
                + super.width + " Length is " + super.length);
    }

}

Notice in BoxWeight class insider method getVariables() we are referring to the variables of parent class by super keyword.

Use of Super to invoke parent class method.

super keyword can also be used to invoke the method of immediate super class. Consider the same Box class given in the previous example, Let's modify our BoxWeight class to invoke the method of Box Class.

public class BoxWeight extends Box {

    int weight;

    public BoxWeight(int height, int width, int length, int weight) {
        super(height, width, length);
        this.weight = weight;
    }

    public void printWeight() {
        System.out.println("Weight is " + weight);
    }

    public void volume() {
        super.calculateVolume();
    }

}
Comments