Java super keyword

The super keyword in Java is a reference variable that is used to refer to the parent class (superclass) objects. It is commonly used to access members (variables, methods) of the superclass from within the subclass. The super keyword serves several purposes and has specific use cases in Java, such as accessing superclass constructors, methods, and fields.

Where to use super keyword?

1. Accessing superclass methods: super is used to call methods of the superclass that are overridden by the subclass.

2. Accessing superclass fields: super is used to access fields of the superclass when they are hidden by subclass fields.

3. Calling superclass constructor: super() is used to call the constructor of the superclass.

Accessing superclass methods

If a method in a subclass overrides a method in the superclass, you can use super to call the superclass’s method.

Example:


//Main.java file
class Animal {
    public void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    public void sound() {
        super.sound();  // Calling superclass method
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.sound();
    }
}

Output:

Animal makes a sound
Dog barks

Explanation:

  1. The Dog class overrides the sound() method of the Animal class.
  2. Inside the sound() method of Dog, super.sound() calls the sound() method from the Animal class before printing “Dog barks”.
  3. This allows the subclass to enhance or modify the behavior while still preserving the behavior of the superclass method.

Accessing Superclass Fields

If a field in the subclass has the same name as a field in the superclass, the field in the subclass will hide the field in the superclass. You can use super to refer to the hidden field of the superclass.

Example:


//Main.java file
class Animal {
    String name = "Animal";
}

class Dog extends Animal {
    String name = "Dog";

    public void getNames() {
        System.out.println("Superclass name: " + super.name);  // Accessing superclass field
        System.out.println("Subclass name: " + name);         // Accessing subclass field
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.getNames();
    }
}

Output:

Superclass name: Animal
Subclass name: Dog

Explanation:

  1. The Dog class has a field name, which hides the name field in the Animal class.
  2. Using super.name, we access the name field of the superclass (Animal), while name without super refers to the name field of the subclass (Dog).

Calling Superclass Constructor

In Java, a subclass constructor automatically calls the no-argument constructor of its superclass. However, if you want to call a constructor of the superclass that takes arguments, you can use the super() keyword with appropriate arguments.

Example:


//Main.java file
class Animal {
    String name;

    // Constructor of the superclass
    public Animal(String name) {
        this.name = name;
        System.out.println("Animal constructor called");
    }
}

class Dog extends Animal {
    int age;

    // Constructor of the subclass
    public Dog(String name, int age) {
        super(name);  // Calling superclass constructor
        this.age = age;
        System.out.println("Dog constructor called");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Buddy", 5);
    }
}

Output:

Animal constructor called
Dog constructor called

Explanation:

  1. The Animal class has a constructor that takes a String parameter name.
  2. The Dog class constructor takes two parameters (name and age) and calls the Animal class constructor using super(name) to initialize the name field in the superclass.
  3. This ensures that the Animal class is properly initialized before the Dog class constructor runs.

Accessing Superclass Constructor (No-argument Constructor)

If the superclass has a no-argument constructor, and the subclass constructor doesn’t explicitly call super(), Java automatically calls the superclass’s no-argument constructor.

Example:


//Main.java file
class Animal {
    public Animal() {
        System.out.println("Animal constructor called");
    }
}

class Dog extends Animal {
    public Dog() {
        // super() is automatically called here to invoke Animal() constructor
        System.out.println("Dog constructor called");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
    }
}

Output:

Animal constructor called
Dog constructor called

Explanation:

  1. In this case, the Dog constructor does not explicitly call super(), so Java automatically calls the no-argument constructor of the superclass (Animal).
  2. This is the default behavior if there is no explicit call to the superclass constructor.