Java super Keyword
Introduction to super Keyword
The super keyword in Java is used to refer to the immediate parent class. It is mainly used for:
1. Accessing parent class fields and methods hidden by subclass members with the same name
2. Calling parent class constructors from subclass constructors
3. Differentiating between superclass and subclass members when they share the same identifier
Uses of super Keyword
Usage | Syntax | Description |
---|---|---|
Access superclass variable | super.variableName | Accesses the hidden variable from the superclass |
Call superclass method | super.methodName() | Invokes the overridden method from the superclass |
Call superclass constructor | super() or super(params) | Must be the first statement in a subclass constructor |
Accessing Superclass Members
If a subclass defines members with the same name as those in its superclass, the super keyword allows direct access to the superclass version of those members.
class Animal {
protected String name = "Animal";
public void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
private String name = "Dog"; // Hides superclass variable
public void displayNames() {
System.out.println("Subclass name: " + name);
System.out.println("Superclass name: " + super.name); // Access superclass variable
}
@Override
public void makeSound() {
System.out.println("Dog barks");
}
public void makeAnimalSound() {
super.makeSound(); // Call superclass method
}
}
public class SuperExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.displayNames();
dog.makeSound();
dog.makeAnimalSound();
}
}
Subclass name: Dog Superclass name: Animal Dog barks Animal makes a sound
Using super() to Call Superclass Constructors
The call to super() must be the first statement in a subclass constructor. If it is omitted, the compiler automatically inserts a call to the no-argument constructor of the superclass.
class Person {
protected String name;
protected int age;
public Person() {
System.out.println("Person no-arg constructor called");
}
public Person(String name, int age) {
this.name = name;
this.age = age;
System.out.println("Person parameterized constructor called");
}
}
class Student extends Person {
private int studentId;
public Student() {
// super() is implicitly called here
System.out.println("Student no-arg constructor called");
}
public Student(String name, int age, int studentId) {
super(name, age); // Must be the first statement
this.studentId = studentId;
System.out.println("Student parameterized constructor called");
}
public void display() {
System.out.println("Name: " + name + ", Age: " + age + ", ID: " + studentId);
}
}
public class SuperConstructorExample {
public static void main(String[] args) {
System.out.println("Creating student with no args:");
Student student1 = new Student();
System.out.println("\nCreating student with parameters:");
Student student2 = new Student("Alice", 20, 12345);
student2.display();
}
}
Creating student with no args: Person no-arg constructor called Student no-arg constructor called Creating student with parameters: Person parameterized constructor called Student parameterized constructor called Name: Alice, Age: 20, ID: 12345
super in Multilevel Inheritance
In multilevel inheritance, the super keyword always refers to the immediate parent class, not ancestors further up the hierarchy.
class Grandparent {
protected String message = "Grandparent message";
}
class Parent extends Grandparent {
protected String message = "Parent message";
}
class Child extends Parent {
private String message = "Child message";
public void displayMessages() {
System.out.println("Child: " + message);
System.out.println("Parent: " + super.message); // Immediate parent
// To access the grandparent's message, explicit casting or helper methods would be needed
}
}
public class MultilevelSuper {
public static void main(String[] args) {
Child child = new Child();
child.displayMessages();
}
}
Child: Child message Parent: Parent message
Best Practices
- Use super to resolve naming conflicts between subclass and superclass members
- Always call the appropriate superclass constructor using super() when subclass constructors require it
- Remember that super() must appear as the first statement in a constructor
- Use super.method() to extend superclass functionality without fully replacing it
- Avoid overly deep inheritance hierarchies, as managing super calls can become complex