Inheritance and Polymorphism

Dive deeper into inheritance, its benefits, and different types (single, multiple, multilevel). Understand polymorphism (method overriding and overloading) and its applications.


The 'super' Keyword in Java

Explanation: The 'super' Keyword

In Java, the super keyword is a reference variable that is used to refer to the immediate parent class object. It's primarily used in the following situations:

  • Accessing parent class members (methods and variables): When a subclass overrides a method or hides a variable from its parent class, super allows you to specifically access the parent's version.
  • Calling the parent class's constructor: This is crucial for initializing the inherited properties from the parent class.

Think of super as a way to say, "Hey, Java, I want to refer to something in my parent class specifically, even if I've redefined it in my own class."

Using 'super' to Access Parent Class Members

Let's illustrate how super allows you to access methods and variables from the parent class within a subclass. Here's a Java code example:

 class Animal {
    String name = "Generic Animal"; // Variable in parent class

    void makeSound() {
        System.out.println("Generic animal sound");
    }
}

class Dog extends Animal {
    String name = "Doggy";  // Hides the parent's 'name' variable

    @Override
    void makeSound() {
        System.out.println("Woof!");
    }

    void displayInfo() {
        System.out.println("My name is: " + name);        // Accessing the Dog's 'name'
        System.out.println("My parent's name is: " + super.name); // Accessing the Animal's 'name'
        super.makeSound();  // Calling the Animal's makeSound() method
    }


    // Example of using super() in a constructor
    Dog(String dogName) {
        //this.name = dogName; // Initialize dog's name, if not hiding parent's

        //You can also change parent's name like this:
        //super.name = dogName;

    }
}


public class SuperExample {
    public static void main(String[] args) {
        Dog myDog = new Dog("Buddy");
        myDog.displayInfo();
    }
} 

Explanation of the Code:

  • Animal Class: This is the parent class with a variable name and a method makeSound().
  • Dog Class: This is the subclass of Animal. It also has a variable name (which hides the parent's name). It overrides the makeSound() method.
  • displayInfo() Method: This method demonstrates the use of super.
    • System.out.println("My name is: " + name); accesses the name variable of the Dog class.
    • System.out.println("My parent's name is: " + super.name); accesses the name variable of the Animal class using super.
    • super.makeSound(); calls the makeSound() method of the Animal class.
  • Dog(String dogName) Constructor: (Not explicitly used in the main, but shows usage) Demonstrates the use of `super()` to potentially initialize or modify the parent class properties during object construction.

Output:

 My name is: Doggy
My parent's name is: Generic Animal
Generic animal sound 

As you can see, super allows us to differentiate between the subclass's members and the parent class's members, providing precise control when inheritance is involved.

Key Takeaways

  • super refers to the immediate parent class.
  • It allows you to access overridden methods or hidden variables from the parent.
  • It's essential for properly initializing parent class properties within subclass constructors.
  • Improves code clarity and avoids ambiguity when dealing with inheritance.