Mastering Java's super Keyword: Advanced Usage & Practical Examples
Mastering Java’s super Keyword: Advanced Usage & Practical Examples
Discover how the super keyword unlocks powerful inheritance capabilities in Java, with clear examples and expert guidance.
The super keyword is a cornerstone of Java’s inheritance model, allowing subclasses to reference members—fields, methods, and constructors—of their direct superclass. Understanding its nuances is essential for writing clean, maintainable code.
Primary Uses of super
- Invoke overridden superclass methods.
- Access superclass fields when shadowed by subclass fields.
- Explicitly call a superclass constructor from a subclass constructor.
Let’s dive into each scenario with code samples.
1. Calling an Overridden Superclass Method
When a subclass overrides a method, the superclass version remains accessible via super.methodName(). This is critical for extending behavior without losing the original functionality.
Example: Method Overriding and super
class Animal {
public void display() {
System.out.println("I am an animal");
}
}
class Dog extends Animal {
@Override
public void display() {
System.out.println("I am a dog");
}
public void printMessage() {
display(); // Calls Dog.display()
super.display(); // Calls Animal.display()
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.printMessage();
}
}
Output:
I am a dog I am an animal
Using super.display() ensures the superclass implementation is executed, which is useful when extending functionality rather than replacing it.
2. Accessing Shadowed Superclass Fields
If both superclass and subclass declare a field with the same name, the subclass field shadows the superclass one. super.fieldName lets you reference the original field.
Example: Field Shadowing
class Animal {
protected String type = "animal";
}
class Dog extends Animal {
public String type = "mammal";
public void printType() {
System.out.println("I am a " + type); // Dog.type
System.out.println("I am an " + super.type); // Animal.type
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.printType();
}
}
Output:
I am a mammal I am an animal
3. Using super() to Call a Superclass Constructor
Constructors are not inherited, but a subclass can invoke a superclass constructor using super(). This must be the first statement in the subclass constructor and is required when the superclass lacks a no‑arg constructor or when you need to pass arguments.
Example 1: Default Constructor Call
class Animal {
Animal() {
System.out.println("I am an animal");
}
}
class Dog extends Animal {
Dog() {
super();
System.out.println("I am a dog");
}
}
public class Main {
public static void main(String[] args) {
new Dog();
}
}
Output:
I am an animal I am a dog
Example 2: Parameterized Constructor Call
class Animal {
Animal(String type) {
System.out.println("Type: " + type);
}
}
class Dog extends Animal {
Dog() {
super("Animal");
System.out.println("I am a dog");
}
}
public class Main {
public static void main(String[] args) {
new Dog();
}
}
Output:
Type: Animal I am a dog
In the parameterized case, the compiler cannot invoke a non‑default constructor automatically, so super(arguments) is mandatory.
Mastering the super keyword empowers you to write robust inheritance hierarchies, ensuring that overridden logic remains accessible and constructors are properly initialized. These techniques are essential for scalable Java applications and align with best practices recommended by industry experts.
Java
- Master Java Operators: Types, Syntax, & Practical Examples
- Java Comments: Types, Usage, and Best Practices
- Mastering Java if…else: Control Flow Explained
- Mastering the Java Enhanced For Loop: Syntax, Examples, and Best Practices
- Java Break Statement: How, When, and Labeled Breaks Explained
- Mastering Java Arrays: Declaration, Initialization, and Manipulation
- Java Methods: How to Define, Call, and Use Them Effectively
- Mastering Java Interfaces: Concepts, Implementation, and Best Practices
- Mastering Java Try‑with‑Resources: Automatic Resource Management Explained
- Java Annotations Explained: Types, Placement, and Practical Examples