Java super Keyword


In Java, the super keyword is used to refer to the superclass (parent class) of the current object. It serves as a reference to the superclass and is commonly used to access superclass members (fields, methods, and constructors) from the subclass. The super keyword is vital in scenarios involving method overriding, constructor chaining, and when you need to access hidden members in the subclass.

This guide will walk you through the uses and concepts surrounding the super keyword in Java with examples.

Table of Contents

  1. What is the super Keyword?
  2. Using super to Call Superclass Methods
  3. Using super to Access Superclass Fields
  4. Using super() to Call Superclass Constructor
  5. Difference Between super and this
  6. Common Use Cases of super
  7. Advantages of Using super

What is the super Keyword?

In Java, the super keyword refers to the superclass of the current object. It can be used in several contexts, such as:

  • To call a superclass method that has been overridden in the subclass.
  • To access a superclass field that has been hidden in the subclass.
  • To call a constructor of the superclass.

The super keyword helps distinguish between the members of the superclass and the subclass, especially in cases where they have the same names.


Using super to Call Superclass Methods

When a subclass overrides a method of its superclass, you can use the super keyword to call the original method from the superclass. This is useful when you want to add new functionality in the subclass while retaining the functionality from the superclass.

Example:

class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    @Override
    void sound() {
        super.sound(); // Calls the 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

In this example, the Dog class overrides the sound() method from the Animal class. The super.sound() call in the Dog class invokes the sound() method from the superclass before executing its own implementation.


Using super to Access Superclass Fields

If a subclass has a field with the same name as a field in its superclass, the subclass field hides the superclass field. To access the superclass field, you can use the super keyword.

Example:

class Animal {
    String name = "Animal";
}

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

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

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

Output:

Superclass name: Animal
Subclass name: Dog

Here, the super.name is used to access the name field of the Animal class (the superclass), while name directly refers to the name field of the Dog class (the subclass).


Using super() to Call Superclass Constructor

The super() keyword is used to call the constructor of the superclass. It is used in the subclass constructor to initialize the superclass part of the object. If no constructor is explicitly defined in the superclass, the default constructor is called automatically.

Example:

class Animal {
    Animal() {
        System.out.println("Animal constructor");
    }
}

class Dog extends Animal {
    Dog() {
        super(); // Calls the superclass constructor
        System.out.println("Dog constructor");
    }
}

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

Output:

Animal constructor
Dog constructor

In this example, the Dog class constructor calls super() to invoke the constructor of the Animal class (the superclass) before executing its own code.


Difference Between super and this

While both super and this are used to refer to the object, they serve different purposes:

  • super: Refers to the superclass (parent class).
    • Used to access superclass methods and fields.
    • Used to call the superclass constructor.
  • this: Refers to the current object (instance of the class).
    • Used to refer to the current class's instance variables and methods.
    • Used to call the current class's constructor.

Example:

class Animal {
    String name = "Animal";
}

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

    void displayNames() {
        System.out.println("Superclass name: " + super.name); // Refers to superclass field
        System.out.println("Subclass name: " + this.name);   // Refers to subclass field
    }
}

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

Output:

Superclass name: Animal
Subclass name: Dog

In this case, super.name refers to the name field in the superclass Animal, while this.name refers to the name field in the Dog class.


Common Use Cases of super

The super keyword is used in several scenarios:

  1. Calling a Superclass Method: To invoke a method from the superclass that has been overridden in the subclass.
  2. Accessing Superclass Fields: To access a field of the superclass that has been hidden in the subclass.
  3. Calling a Superclass Constructor: To invoke the constructor of the superclass from the subclass.
  4. Handling Constructor Chaining: In complex class hierarchies, super() is used to ensure the proper initialization of both the superclass and subclass.

Advantages of Using super

  1. Clearer Code: Using super makes it clear that you are intentionally accessing a superclass member or constructor.
  2. Avoiding Name Conflicts: super helps resolve ambiguity when a subclass hides a superclass member (field or method).
  3. Constructor Chaining: By using super(), you can ensure proper initialization of the superclass part of an object in a constructor.