zaro

What is the super keyword in Java?

Published in Java Keywords 4 mins read

The super keyword in Java is a reference variable used within a subclass to refer to its immediate parent class (superclass) object. It allows a subclass to access members (fields and methods) of its superclass, particularly when these members have been overridden or hidden in the subclass.

Understanding the super Keyword's Core Functionality

The primary role of the super keyword is to establish a link from a subclass to its direct parent class. This is crucial in object-oriented programming, especially with inheritance, enabling subclasses to interact with their inherited components.

Key aspects of the super keyword:

  • Immediate Parent Reference: super always refers to the immediate superclass, not a grandparent or any other ancestor in the hierarchy.
  • Accessibility: It enables access to members (variables, methods, and constructors) of the superclass that might otherwise be masked by same-named members in the subclass.
  • Contextual Use: The super keyword can only be used within the instance methods or constructors of a subclass.

For more details on inheritance and the super keyword, you can refer to the official Java documentation on inheritance.

Common Uses of the super Keyword

The super keyword is primarily used in two scenarios:

1. Invoking Superclass Constructors

A subclass constructor can use super() to call a constructor of its superclass. This is essential for initializing the superclass's portion of the object.

  • Syntax: super() or super(arguments)
  • Rule: A call to super() (or super(arguments)) must be the first statement within the subclass's constructor. If no super() call is explicitly made, the compiler automatically inserts a default super() (no-argument constructor) call.

Example:

class Vehicle {
    String type;

    Vehicle(String type) {
        this.type = type;
        System.out.println("Vehicle constructor: " + type);
    }
}

class Car extends Vehicle {
    String model;

    Car(String type, String model) {
        super(type); // Calls the Vehicle constructor
        this.model = model;
        System.out.println("Car constructor: " + model);
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Sedan", "Civic");
        // Output:
        // Vehicle constructor: Sedan
        // Car constructor: Civic
    }
}

2. Accessing Superclass Members (Fields and Methods)

When a subclass declares a field or method with the same name as one in its superclass, the superclass member is said to be "hidden" (for fields) or "overridden" (for methods). The super keyword provides a way to refer to the superclass's member.

Accessing Superclass Methods

To call an overridden method of the superclass from within the subclass:

  • Syntax: super.methodName(arguments)

Example:

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

class Dog extends Animal {
    @Override
    void makeSound() {
        super.makeSound(); // Calls Animal's makeSound()
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.makeSound();
        // Output:
        // Animal makes a sound
        // Dog barks
    }
}

Accessing Superclass Fields

To access a field from the superclass that is hidden by a same-named field in the subclass:

  • Syntax: super.fieldName

Example:

class Parent {
    String name = "Parent Name";
}

class Child extends Parent {
    String name = "Child Name"; // Hides Parent's name field

    void displayNames() {
        System.out.println("Child's name: " + this.name);
        System.out.println("Parent's name: " + super.name); // Accesses Parent's name
    }
}

public class Main {
    public static void main(String[] args) {
        Child myChild = new Child();
        myChild.displayNames();
        // Output:
        // Child's name: Child Name
        // Parent's name: Parent Name
    }
}

super vs. this Keyword Comparison

Understanding the difference between super and this is fundamental in Java:

Feature this Keyword super Keyword
Reference To Current object (instance of the class) Immediate parent class object
Use Case Call current class constructor, access current class members Call parent class constructor, access parent class members
Context Refers to members within the current class Refers to members within the superclass
Constructor Call this() or this(args) (must be first statement) super() or super(args) (must be first statement)

Practical Insights and Best Practices

  • Clarity in Inheritance: Using super explicitly makes your code clearer about whether you're calling a superclass's version of a method or a subclass's version.
  • Constructor Chaining: It's vital for proper object initialization across the inheritance hierarchy, ensuring that all parts of the object (from parent to child) are set up correctly.
  • Avoiding Redundancy: Instead of re-implementing logic, you can reuse superclass behavior by calling its methods via super.