What is the purpose of the super keyword?

Beginner

Answer

The super keyword refers to the immediate parent class object and is used to:

  1. Call parent class constructor
  2. Access parent class methods
  3. Access parent class variables
class Parent {
    String name = "Parent";
    void display() { System.out.println("Parent display"); }
}
class Child extends Parent {
    String name = "Child";
    Child() {
        super(); // Call parent constructor
    }
    void display() {
        super.display(); // Call parent method
        System.out.println("Parent name: " + super.name);
    }
}