Unit 9: Inheritance

Superclasses and Subclasses in Java

Introduction to Inheritance

At the heart of object-oriented programming lies the concept of inheritance. It enables a new class to inherit properties and behaviors from an existing class, fostering code reusability and establishing a natural hierarchy between classes.


The Fundamentals of Inheritance

What are Superclasses and Subclasses?

  • Superclass: Also known as a parent or base class, it's the class from which another class inherits properties and behaviors.
  • Subclass: Also referred to as a child or derived class, it inherits properties and behaviors from another class, its superclass.

Establishing an Inheritance Relationship

In Java, the extends keyword is used to create a subclass from a superclass.

class Animal {
   void eat() {
      System.out.println("This animal eats food");
   }
}

class Dog extends Animal {
   void bark() {
      System.out.println("The dog barks");
   }
}

In the above example, Dog is the subclass derived from the Animal superclass.

Key Insight

When a subclass is derived from a superclass, it inherits all the non-private members (fields, methods) of the superclass. However, constructors are not inherited.

The super Keyword

The super keyword in Java is a reference variable that refers to the immediate parent class object. It can be used in two primary scenarios:

  1. To call the parent class method.

    class Dog extends Animal {
       void eat() {
          super.eat();
          System.out.println("The dog eats dog food");
       }
    }
    
  2. To call the parent class constructor.

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

Best Practice

Always use the super() call to invoke the parent class's constructor explicitly, ensuring the proper initialization of the superclass object.


Advantages of Inheritance

  • Code Reusability: Inheritance allows the reuse of methods and fields of the parent class.
  • Method Overriding: A subclass can provide a specific implementation for a method that's already defined in its superclass.
  • Establish Hierarchies: Establishes a natural class hierarchy mirroring real-world systems.

Summary

Inheritance in Java, encapsulated by superclasses and subclasses, is a cornerstone of object-oriented programming. It facilitates code reusability, establishes natural hierarchies, and allows for method overriding, enabling Java developers to build efficient and robust applications. Grasping this concept is pivotal for students aiming to ace AP Computer Science A.


References


AP CSA Homework Assignment

Assignment: Hierarchies in the Animal Kingdom

Instructions

  1. Create a superclass named Animal with methods like eat(), sleep(), and sound().
  2. Derive subclasses like Mammal, Bird, and Fish from the Animal superclass.
  3. Override the sound() method in each subclass to provide specific sounds for each animal type.
  4. Further extend the subclasses to specific animals like Dog from Mammal, Sparrow from Bird, etc., and override methods if necessary.
  5. In a main method, create objects of these specific animals and call their methods to see the polymorphic behavior in action.

Through this assignment, students will experience firsthand the hierarchical nature of inheritance and its power in organizing and reusing code in Java.

Previous
Searching and Sorting 2D Arrays