super to Access Superclass Members
In Java, the super
keyword refers to the immediate parent class of a given object. It is used to access members (fields, methods, and constructors) of the parent class from the child class.
Key Uses of super
:
- Accessing Superclass Methods: It is used to call a method of the superclass when the subclass has overridden that method.
- Accessing Superclass Constructor: It is used to call the constructor of the superclass from the subclass.
- Accessing Superclass Fields: It allows access to the fields of the superclass.
Example 1: Using super
to call a superclass method
class Animal {
void eat() {
System.out.println("Animal is eating...");
}
}
class Dog extends Animal {
void eat() {
super.eat(); // Call the eat() method of the parent class Animal
System.out.println("Dog is eating...");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat(); // Output will be: Animal is eating... Dog is eating...
}
}
Example 2: Using super
to call a superclass constructor
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
super(); // Call the constructor of the parent class Animal
System.out.println("Dog constructor");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog(); // Output will be: Animal constructor Dog constructor
}
}
Example 3: Using super
to access a superclass field
class Animal {
String name = "Animal";
}
class Dog extends Animal {
String name = "Dog";
void display() {
System.out.println("Name from Dog class: " + name);
System.out.println("Name from Animal class: " + super.name); // Access the name from Animal class
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.display(); // Output will be: Name from Dog class: Dog Name from Animal class: Animal
}
}