Super Keyword in Java: An In-Depth Guide to Core Concepts and Interview Strategies

In Java, super is a powerful keyword used to access members of a class’s parent (or superclass). It provides a way for subclasses to directly interact with the variables, methods, and constructors of their superclass. Here’s a detailed look at the different ways the super keyword in Java is utilized:

Accessing Superclass Methods and Variables

In Java, when a subclass inherits from a superclass, it may override methods or redefine instance variables. If the subclass needs to call the superclass version of a method or access the superclass version of a variable, the super keyword comes into play.

Example:

class RBI {
    String name = "RBI";

    void rules() {
        System.out.println("RBI has its own logo");
    }
}

class ICICI extends RBI {
    String name = "ICICI";

    void rules() {
        System.out.println("ICICI will have its own logo");
    }

    void printName() {
        System.out.println("Subclass name: " + name);
        System.out.println("Superclass name: " + super.name);
    }

    void parentRules() {
        super.rules(); // Calls the superclass method
    }
}

In this example:
The super.name accesses the name variable of the superclass RBI, while the name without super refers to the ICICI class’s name.
super.rules() calls the rules method from the superclass RBI, overriding the ICICI class’s rules method.

Calling the Superclass Constructor

Another important use of super in Java is to call the superclass constructor. When a subclass constructor is invoked, it implicitly calls the no-argument constructor of its superclass unless specified otherwise. You can explicitly call a parameterized constructor of the superclass using super.

Example:

class RBI {
    RBI(String name) {
        System.out.println("RBI constructor called. Name: " + name);
    }
}

class ICICI extends RBI {
    ICICI(String name) {
        super(name); // Calls superclass constructor
        System.out.println("ICICI constructor called.");
    }
}

In this example, the ICICI constructor uses super(name) to call the parameterized constructor of the superclass RBI. This is one of the key use of super keyword in java.

Important Note:

We need to explicitly call the parent class’s parameterized constructor here. Otherwise, a compile-time error will occur. This happens because the parent class RBI does not have a default no-argument constructor, but only a parameterized constructor.

Why does this happen?

By default, the JVM creates a no-argument constructor for a class if no constructors are defined. If you provide a constructor that includes parameters, the JVM will not create a default no-argument constructor. This means if the child class does not explicitly call the superclass’s parameterized constructor, it will attempt to call the no-argument constructor, which does not exist in this case, resulting in a compile-time error.

To fix this issue, you can:

  • Implement a no-argument constructor in the parent class, or
  • Guarantee that the subclass invokes the superclass’s parameterized constructor using super()

Accessing Superclass Methods in Overridden Context

When we have a scenario where the child class overrides the method of its parent class and wants to invoke the parent class’s version of the method, the super keyword is used to accomplish this. This allows the child class to extend the functionality of the method rather than completely replacing it.

Example:

class RBI {
    void empPolicy() {
        System.out.println("RBI has its own employee policy");
    }
}

class ICICI extends RBI {
    @Override
    void empPolicy() {
        super.empPolicy(); // Calls the superclass method
        System.out.println("ICICI has its own employee policy.");
    }
}

In this example, the empPolicy method in the ICICI class overrides the empPolicy method in the RBI class. However, by using super.empPolicy(), it still calls the RBI version of the method before adding its own logic.

Working with Multiple Inheritance

Java doesn’t support multiple inheritance with classes, but it does allow classes to implement multiple interfaces. In cases where class hierarchies become complex, especially in frameworks, the super keyword becomes critical to ensure the correct parent class’s methods and properties are accessed.

Key Considerations:

  • The super keyword in Java must always appear as the first line of code in a constructor when calling a superclass constructor. If you try to use the super keyword in any other place within the constructor, it will result in a compile-time error.
Example:

class RBI {
    RBI(String name) {
        System.out.println("RBI constructor called. Name: " + name);
    }
}

/* Wrong way to use super Keyword: */

class ICICI extends RBI {
    ICICI(String name) {
        super(name); // Calls the superclass constructor
        System.out.println("ICICI constructor called.");
    }
}

/* Correct way to use super Keyword: */

class ICICI extends RBI {
    ICICI(String name) {
        System.out.println("ICICI constructor called.");
        super(name); // Compile-time error
    }
}

/* In the incorrect example, placing the super(name) after another statement will cause a compile-time error because super() must always be the first statement in a constructor. */
  • The super keyword is applicable only within a subclass’s constructor for calling the constructor of its superclass. It can only be used within a constructor to invoke the parent class’s constructor. Attempting to use it elsewhere will result in a compile-time error.
Example:

class RBI {
    RBI(String name) {
        System.out.println("RBI constructor called. Name: " + name);
    }
}

class ICICI extends RBI {
    ICICI(String name) {
       /* super(name); */ // Correct usage inside constructor
        System.out.println("ICICI constructor called.");
    }
    
    public void empPolicy() {
        super(name); // Compile-time error: cannot call a superclass constructor here
    }
}

/* In this example, trying to use super(name) inside the empPolicy method results in a compile-time error because constructors cannot be called outside of another constructor. */
  • The super keyword in Java is essential in overriding scenarios when you want to call a superclass’s method from an overridden method.

When to Use Super Keyword in Java?

  • To resolve name conflicts: If the subclass has variables or methods with the same name as those in the superclass, super helps differentiate which one you want to use.
  • Constructor chaining: When a subclass constructor needs to call a specific superclass constructor, super ensures that the correct constructor is invoked
  • To extend functionality: When overriding a method, super can call the original implementation from the superclass, allowing the subclass to add to the existing behavior instead of replacing it entirely.

Conclusion

The super keyword in Java is fundamental for managing inheritance relationships. It allows subclasses to interact directly with their superclass’s fields, methods, and constructors, ensuring a clear and structured way to extend and modify class behavior. Understanding how and when to use super is key to mastering Java’s object-oriented principles.

Interview Tips:

  • Explain the Uses of super: Be prepared to discuss how the super keyword is used for:
    • Calling the parent class constructor.
    • Accessing parent class variables.
    • Implementing constructor chaining.
  • Discuss JVM Behavior: Understand and explain the implicit uses of super in the JVM, especially in scenarios where the parent class does not have a no-argument constructor.
  • Demonstrate Core Concepts: Clearly articulate these points to showcase your solid grasp of Java principles, leaving a strong impression on the interviewer.

2 thoughts on “Super Keyword in Java: An In-Depth Guide to Core Concepts and Interview Strategies”

Leave a Comment