Super Keyword
Similarly, like the this keyword which holds the reference of the current object, the super keyword refers to the parent class (superclass) object from its immediate child class or subclass.
- It is used to call the parent class constructor and method
- It is used to access the parent class varibales
1. Used with constructor :
You can call a superclass constructor from a child class using the super keyword.
Similarly, for constructor chaining within the same class, we use the this keyword to call another constructor in the same class. Likewise, for constructor chaining between a parent and a child class, we use the super keyword to call a constructor of the superclass from the child class.
Example-1:
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
// Calls the constructor of the parent class (Animal)
super(); //If you don't explicitly write super(), Java inserts it automatically.
System.out.println("Dog constructor");
}
}
public class HelloWorld {
public static void main(String[] args) {
// Creates a Dog object, which first invokes the Animal constructor
// through super(), and then executes the Dog constructor
Dog d = new Dog();
}
}
//output
//Animal constructor
//Dog constructorNote : If you don't explicitly write super(), Java inserts it automatically.
Calling Parameterized Parent Constructors Example-2
class Person {
String name;
// Parameterized constructor of Person class
Person(String name) {
// Initializes the instance variable of Person using "this"
this.name = name;
}
}
// Employee class inherits Person class
class Employee extends Person {
int id;
// Parameterized constructor of Employee class
Employee(String name, int id) {
// Calls the parameterized constructor of the parent class (Person)
// to initialize the inherited variable "name"
super(name);
// Initializes the instance variable of Employee
this.id = id;
}
void display() {
System.out.println(name);
System.out.println(id);
}
}
public class helloWorld {
public static void main(String[] args) {
// Creates an Employee object and calls parameterized constructors
// First Person(String name) via super(), then Employee(String name, int id)
Employee e = new Employee("John", 101);
e.display();
}
}
//output:
//John
//1012. Used with method :
In inheritance, when the superclass method and subclass method have the same name, an ambiguity arises. In this situation, if we want to call the parent class method, we can use the super keyword to invoke the superclass method.
Example-1:
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
void showBothSounds() {
sound(); // Current class method
super.sound(); // Parent class method
}
}
public class HelloWorld {
public static void main(String[] args) {
Dog d = new Dog();
d.showBothSounds();
}
}
//output
//Dog barks
//Animal makes sound
Calling Parameterized Parent Methods. Example-2
class Animal {
// Parameterized method in parent class
void sound(String type) {
System.out.println("Animal sound type: " + type);
}
}
class Dog extends Animal {
// Parameterized method in child class
void sound(String type) {
// Calling parent class parameterized method using super keyword with argument
super.sound("Generic Animal");
// Child class specific behavior
System.out.println("Dog sound type: " + type);
}
}
public class Main {
public static void main(String[] args) {
// Creating object of Dog class
Dog d = new Dog();
// Calling parameterized method with argument
d.sound("Bark");
}
}
//output:
//Animal sound type: Generic Animal
//Dog sound type: Bark
3. Used with variables:
In inheritance, when the superclass variable and subclass variable have the same name, then in the subclass the subclass variable hides the parent class variable.
In this situation, we can use the super keyword to access the superclass variable.
Example:
class Animal {
String name = "Animal"; // Parent class variable
}
class Dog extends Animal {
String name = "Dog"; // Child class variable (hides parent class variable)
void printNames() {
// Prints the child class variable "name"
System.out.println(name);
// Uses super keyword to access the parent class variable "name"
System.out.println(super.name);
}
}
public class HelloWorld {
public static void main(String[] args) {
// Creates an object of Dog class
Dog d = new Dog();
// Calls method to print both child and parent class variables
d.printNames();
}
}
//output :
//Dog
//Animal
See you in next page....🙂