Unit 9: Inheritance

Understanding the super Keyword in Java

Introduction to super

The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. It plays a fundamental role in the realm of inheritance, ensuring that subclasses can interact with and refer to members (fields and methods) of their superclasses.


Deciphering the Use of super

Referring to the Superclass Instance Variables

When a subclass contains a member with the same name as a member in its superclass, the super keyword is used to refer to the member of the superclass.

class Animal {
   String color = "White";
}

class Dog extends Animal {
   String color = "Black";

   void displayColor() {
      System.out.println(color);             // prints Black
      System.out.println(super.color);      // prints White
   }
}

Invoking the Superclass Methods

In scenarios where the subclass has overridden one or more methods of its superclass, the super keyword allows us to call the overridden method from the superclass.

class Animal {
   void eat() {
      System.out.println("Eating...");
   }
}

class Dog extends Animal {
   void eat() {
      System.out.println("Eating dog food...");
   }

   void displayEat() {
      eat();
      super.eat();
   }
}

Using super to Call Superclass Constructors

One of the most common usages of super is to call the superclass's constructor.

class Animal {
   Animal() {
      System.out.println("Animal is created");
   }
}

class Dog extends Animal {
   Dog() {
      super();
      System.out.println("Dog is created");
   }
}

Key Insight

The super() call must always be the first statement in the derived class's constructor.


Practical Applications

Using the super keyword can lead to:

  • Cleaner Code: Avoid variable shadowing and make it clear when superclass members are being referred to.
  • More Efficient Overriding: Specifically call superclass methods when needed, even after overriding.
  • Structured Constructors: Ensure the correct sequence of constructor calls in an inheritance hierarchy.

Summary

The super keyword is an integral part of Java's inheritance mechanism, granting subclasses a way to reference their superclass's members. Properly understanding and utilizing super is essential for any student aiming to excel in AP Computer Science A, as it forms the backbone of many object-oriented principles in Java.


References


AP CSA Homework Assignment

Assignment: Deep Dive into super

Instructions

  1. Create a superclass Vehicle with attributes like brand and speed, and methods such as accelerate().
  2. Derive a subclass Car from Vehicle. Ensure that the Car class has the same attributes and methods, but different values or implementations.
  3. Use the super keyword to differentiate between the superclass and subclass members.
  4. Implement scenarios where:
    • The Car class's constructor calls the Vehicle class's constructor.
    • The accelerate() method in Car calls the accelerate() method in Vehicle using super.

By completing this assignment, students will solidify their understanding of the super keyword, its applications, and its significance in Java's inheritance model.

Previous
Interfaces