Super Keyword in Java

In Java, the super keyword is used to refer to the superclass of a subclass. It is used to access the members of the superclass from a subclass. The super keyword can be used in two ways:

  1. To call a constructor of the superclass When creating an object of a subclass, the constructor of the superclass is called first. If you want to call a specific constructor of the superclass, you can use the super keyword followed by the arguments for that constructor. This is done in the first line of the subclass constructor.

For example:

public class SubClass extends SuperClass {
    public SubClass(int arg1, int arg2) {
        super(arg1, arg2);
        // rest of the subclass constructor code
    }
}

Here, the super(arg1, arg2) statement is used to call the constructor of the superclass that takes two arguments.

  1. To refer to a member of the superclass If a subclass has a member with the same name as a member of its superclass, you can use the super keyword to refer to the member of the superclass. This is done by prefixing the member name with the super keyword.

For example:

public class SuperClass {
    protected int number = 10;
}

public class SubClass extends SuperClass {
    int number = 20;
    
    public void printNumber() {
        System.out.println("number in SubClass: " + number);
        System.out.println("number in SuperClass: " + super.number);
    }
}

Here, the printNumber() method in the SubClass prints the value of the number variable in both the subclass and superclass. The super.number statement is used to refer to the number variable in the superclass.

Usage of Java super Keyword:

In Java, the super keyword is used to access the members of the superclass from a subclass. It is used in two main ways:

  1. Call the constructor of the superclass When creating an object of a subclass, the constructor of the superclass is called first. If you want to call a specific constructor of the superclass, you can use the super keyword followed by the arguments for that constructor. This is done in the first line of the subclass constructor.

For example:

public class SubClass extends SuperClass {
    public SubClass(int arg1, int arg2) {
        super(arg1, arg2); // call the constructor of SuperClass with two arguments
        // rest of the subclass constructor code
    }
}
  1. Refer to a member of the superclass If a subclass has a member with the same name as a member of its superclass, you can use the super keyword to refer to the member of the superclass. This is done by prefixing the member name with the super keyword.

For example:

public class SuperClass {
    protected int number = 10;
}

public class SubClass extends SuperClass {
    int number = 20;
    
    public void printNumbers() {
        System.out.println("number in SubClass: " + number);
        System.out.println("number in SuperClass: " + super.number);
    }
}

Here, the printNumbers() method in the SubClass prints the value of the number variable in both the subclass and superclass. The super.number statement is used to refer to the number variable in the superclass.

In general, the super keyword is used to access and invoke the members of the superclass in a subclass. It helps to avoid name conflicts and provide access to superclass methods or variables that may not be accessible in the subclass due to encapsulation.

1) super is used to refer immediate parent class instance variable:

Yes, that is correct. In Java, the super keyword is used to refer to the instance variables of the immediate parent class of a subclass. If the subclass has an instance variable with the same name as an instance variable in its immediate parent class, using the super keyword allows you to refer specifically to the parent class’s instance variable.

For example:

public class Animal {
    protected String name;

    public Animal(String name) {
        this.name = name;
    }
}

public class Dog extends Animal {
    private String breed;

    public Dog(String name, String breed) {
        super(name); // calling the constructor of the Animal class with the name parameter
        this.breed = breed;
    }

    public void printInfo() {
        System.out.println("Name: " + super.name); // referring to the name instance variable of the Animal class using the super keyword
        System.out.println("Breed: " + breed);
    }
}

In the above example, the Dog class is a subclass of the Animal class, which has an instance variable name. The Dog class also has an instance variable name, but using the super keyword in the printInfo() method allows us to refer to the name instance variable of the Animal class specifically. This helps to avoid confusion and ambiguity in the code.

2) super can be used to invoke parent class method:

Yes, that is also correct. In addition to referring to the instance variables of the immediate parent class, the super keyword in Java can also be used to invoke a method in the parent class from a subclass. This is useful if the subclass needs to use the functionality provided by a method in the parent class, but also needs to add some additional functionality of its own.

For example:

public class Animal {
    protected String name;

    public Animal(String name) {
        this.name = name;
    }

    public void makeSound() {
        System.out.println("Animal making a sound");
    }
}

public class Dog extends Animal {
    private String breed;

    public Dog(String name, String breed) {
        super(name);
        this.breed = breed;
    }

    @Override
    public void makeSound() {
        super.makeSound(); // invoking the makeSound() method of the Animal class
        System.out.println("Dog barking");
    }
}

In the above example, the Dog class is a subclass of the Animal class, which has a makeSound() method. The Dog class overrides this method with its own implementation, but also wants to call the makeSound() method of the Animal class as part of its own implementation. Using the super keyword allows the Dog class to call the makeSound() method of the Animal class, and then add its own functionality to the method.

Note that the super keyword is used here with the makeSound() method to refer specifically to the makeSound() method of the immediate parent class. If the makeSound() method was overridden by a grandparent class, using super would still invoke the parent class’s version of the method, rather than the grandparent class’s version.

3) super is used to invoke parent class constructor:

Yes, that is correct. In Java, the super keyword can be used to call a constructor in the immediate parent class of a subclass. This is useful if the subclass needs to perform some initialization that is common to both the subclass and the parent class.

When calling the parent class constructor using super, the call to super() must be the first statement in the subclass constructor. The super() call can be followed by any additional initialization that is specific to the subclass.

For example:

public class Animal {
    protected String name;

    public Animal(String name) {
        this.name = name;
    }
}

public class Dog extends Animal {
    private String breed;

    public Dog(String name, String breed) {
        super(name); // calling the constructor of the Animal class with the name parameter
        this.breed = breed;
    }
}

In the above example, the Dog class is a subclass of the Animal class, which has a constructor that takes a name parameter. When creating a Dog object, the Dog constructor calls the Animal constructor using super(name) to set the name parameter, and then sets the breed parameter specific to the Dog class.

Using super to call the parent class constructor allows the Dog class to perform the common initialization provided by the Animal class without duplicating that code in the Dog constructor.

super example: real use

Here’s an example that illustrates the real use of the super keyword in Java:

public class Shape {
    protected int x;
    protected int y;

    public Shape(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void draw() {
        System.out.println("Drawing a shape at (" + x + ", " + y + ")");
    }
}

public class Rectangle extends Shape {
    protected int width;
    protected int height;

    public Rectangle(int x, int y, int width, int height) {
        super(x, y); // calling the constructor of the Shape class with the x and y parameters
        this.width = width;
        this.height = height;
    }

    @Override
    public void draw() {
        super.draw(); // calling the draw() method of the Shape class
        System.out.println("Drawing a rectangle with width " + width + " and height " + height);
    }
}

In this example, we have a Shape class with an x and y position, and a draw() method that outputs the position of the shape. We also have a Rectangle class that extends the Shape class and adds a width and height property specific to rectangles.

When creating a Rectangle object, we need to pass in the x, y, width, and height parameters. The Rectangle constructor calls the Shape constructor using super(x, y) to set the x and y properties, and then sets the width and height properties specific to rectangles.

In the Rectangle class, we also override the draw() method to draw a rectangle with the specific width and height properties, but we still want to output the position of the shape. We can use the super.draw() method to call the draw() method of the Shape class and output the position of the shape before drawing the rectangle.

Using super to call the Shape constructor and the draw() method allows us to reuse the common functionality provided by the Shape class, and add the specific functionality provided by the Rectangle class.