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:
superrefers 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
superin static contexts (not allowed). - β Overusing
superwhen method overriding isnβt required.
π Performance and Memory Implications
superincurs 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
-
Q: What happens if you donβt call
super()in a subclass?
A: Compiler inserts a defaultsuper()if parent has a no-arg constructor. -
Q: Can
superbe used to call grandparent methods?
A: No, it only refers to the immediate parent. -
Q: Can you use
this()andsuper()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
superensures proper parent initialization and access to overridden behavior.- Use it in constructors and methods to maintain clean inheritance hierarchies.
- Avoid misusing
superin 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.