super Keyword in Java: Calling Parent Constructors and Methods Explained

Learn the super keyword in Java for calling parent constructors and methods. Understand syntax, use cases, best practices, and interview questions with examples

By Updated Java + Backend
Illustration for super Keyword in Java: Calling Parent Constructors and Methods Explained

The super keyword in Java is a reference variable used to access parent class members. It plays a crucial role in inheritance, especially when calling parent constructors and overridden methods.


๐Ÿ“Œ What is the super Keyword?

  • Definition: super refers to the immediate parent class object.
  • Why it matters: Ensures proper initialization and allows reusing parent behavior.
  • When to use: When you want to call parent constructors, access overridden methods, or parent class variables.

[Related: link-to-other-article]


๐Ÿ”น Using super to Call Parent Constructors

โœ… Rules:

  • super() must be the first statement in a subclass constructor.
  • If not explicitly called, Java inserts a default super().

๐Ÿ’ป Example:

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

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

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

Output:

Animal constructor
Dog constructor

Parameterized Constructor Example:

class Animal {
    Animal(String name) {
        System.out.println("Animal: " + name);
    }
}

class Dog extends Animal {
    Dog(String name) {
        super(name); // Passes argument to parent constructor
        System.out.println("Dog: " + name);
    }
}

๐Ÿ”น Using super to Call Parent Methods

When a method is overridden, super.methodName() can call the parent version.

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

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

๐Ÿ”น Accessing Parent Variables

class Animal {
    String type = "Animal";
}

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

    void showType() {
        System.out.println(super.type); // Access parent variable
    }
}

๐Ÿ”น Real-World Analogy

Think of super as calling your parents for help. You inherit traits (methods/fields) but can still rely on them to handle certain responsibilities.


๐Ÿšซ Common Mistakes and Anti-Patterns

  • โŒ Forgetting super() in subclasses when parent has no default constructor.
  • โŒ Using super in static contexts (not allowed).
  • โŒ Overusing super when method overriding isnโ€™t required.

๐Ÿ“ˆ Performance and Memory Implications

  • super incurs no extra runtime overhead; itโ€™s resolved at compile-time.
  • Ensures consistent object initialization in inheritance hierarchies.
Usage Impact
Calling constructors Proper object initialization
Calling methods Access to parent behavior

๐Ÿ”ง Best Practices

  • Always call super() explicitly when parent has parameterized constructors.
  • Use super.method() to enhance, not duplicate, parent behavior.
  • Keep constructor chains simple and predictable.

๐Ÿ“š Interview Questions

  1. Q: What happens if you donโ€™t call super() in a subclass?
    A: Compiler inserts a default super() if parent has a no-arg constructor.

  2. Q: Can super be used to call grandparent methods?
    A: No, it only refers to the immediate parent.

  3. Q: Can you use this() and super() in the same constructor?
    A: No, both must be the first statement.


๐Ÿ“Œ Java Version Relevance

Java Version Change
Java 1.0 Introduced super keyword
Java 5 @Override introduced, making super.method() safer

โœ… Conclusion & Key Takeaways

  • super ensures proper parent initialization and access to overridden behavior.
  • Use it in constructors and methods to maintain clean inheritance hierarchies.
  • Avoid misusing super in contexts where composition is better.

โ“ FAQ

Q: Can super be used in static methods?
A: No, it requires an instance context.

Q: Is calling super() mandatory?
A: Only if the parent has no default constructor.

Q: Can super be used in multiple inheritance?
A: Java doesnโ€™t support multiple inheritance for classes, so no.

Part of a Series

This tutorial is part of our Core Java . Explore the full guide for related topics, explanations, and best practices.

View all tutorials in this series โ†’