Introduction
The super
keyword in Java is used to refer to the immediate superclass of the current object. It is a reference variable that is used to access superclass methods, constructors, and fields. The super
keyword plays a vital role in inheritance by allowing subclasses to call superclass methods and constructors and to access superclass fields.
Table of Contents
- What is the
super
Keyword? - Uses of the
super
Keyword - Accessing Superclass Methods
- Accessing Superclass Fields
- Invoking Superclass Constructors
- Real-World Analogy
- Example:
super
Keyword - Conclusion
1. What is the super Keyword?
The super
keyword is a reference variable that refers to the immediate superclass of the current object. It is used to call superclass methods, access superclass fields, and invoke superclass constructors.
2. Uses of the super Keyword
The super
keyword can be used for the following purposes:
- To call a method in the superclass that has been overridden in the subclass.
- To access a field in the superclass that has been hidden by a field in the subclass.
- To invoke a constructor of the superclass.
3. Accessing Superclass() Methods
The super
keyword can be used to call a method in the superclass that has been overridden in the subclass.
Example:
class Animal {
public void makeSound() {
System.out.println("This animal makes a sound.");
}
}
class Dog extends Animal {
@Override
public void makeSound() {
System.out.println("The dog barks.");
}
public void callSuperMethod() {
super.makeSound(); // Calling the superclass method
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Calls the overridden method in Dog class
dog.callSuperMethod(); // Calls the superclass method in Animal class
}
}
Output:
The dog barks.
This animal makes a sound.
4. Accessing Superclass Fields
The super
keyword can be used to access a field in the superclass that has been hidden by a field in the subclass.
Example:
class Animal {
String name = "Animal";
}
class Dog extends Animal {
String name = "Dog";
public void displayNames() {
System.out.println("Name in subclass: " + name); // Accessing the subclass field
System.out.println("Name in superclass: " + super.name); // Accessing the superclass field
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.displayNames();
}
}
Output:
Name in subclass: Dog
Name in superclass: Animal
5. Invoking Superclass Constructors
The super
keyword can be used to invoke a constructor of the superclass. This is particularly useful when you want to initialize the superclass fields from the subclass constructor.
Example:
class Animal {
String name;
// Constructor
public Animal(String name) {
this.name = name;
}
}
class Dog extends Animal {
String breed;
// Constructor
public Dog(String name, String breed) {
super(name); // Calling the superclass constructor
this.breed = breed;
}
public void display() {
System.out.println("Name: " + name);
System.out.println("Breed: " + breed);
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy", "Golden Retriever");
dog.display();
}
}
Output:
Name: Buddy
Breed: Golden Retriever
6. Real-World Analogy
Consider a family hierarchy:
- The superclass represents a parent, and the subclass represents a child.
- The child can access the parent’s properties (fields) and behaviors (methods) using the
super
keyword. - The child can also call the parent’s constructor to initialize inherited properties.
7. Example: super Keyword
Let’s create a comprehensive example to demonstrate the different uses of the super
keyword.
Example: super
Keyword
class Person {
String name;
int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void displayDetails() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
class Employee extends Person {
int employeeId;
// Constructor
public Employee(String name, int age, int employeeId) {
super(name, age); // Calling the superclass constructor
this.employeeId = employeeId;
}
@Override
public void displayDetails() {
super.displayDetails(); // Calling the superclass method
System.out.println("Employee ID: " + employeeId);
}
}
public class Main {
public static void main(String[] args) {
Employee employee = new Employee("Alice", 30, 101);
employee.displayDetails();
}
}
Output:
Name: Alice, Age: 30
Employee ID: 101
In this example:
- The
Person
class is the superclass with fieldsname
andage
, and a methoddisplayDetails
. - The
Employee
class extendsPerson
and adds anemployeeId
field. It overrides thedisplayDetails
method to include the employee ID. - The
super
keyword is used to call the superclass constructor and method from the subclass.
8. Conclusion
The super
keyword in Java is a powerful feature that allows you to refer to the immediate superclass of the current object. It helps to access superclass methods, fields, and constructors, promoting code reuse and flexibility in inheritance. Understanding and using the super
keyword can help create more readable, maintainable, and extensible Java code.