Unit 9: Inheritance

This unit explores inheritance in Java, a fundamental concept of object-oriented programming that allows for code reuse and establishing hierarchical relationships between classes.

Key Concepts

  • Inheritance: The mechanism by which a class (subclass) can inherit properties and methods from another class (superclass).
  • Superclass (Parent Class): The class whose features are inherited by another class.
  • Subclass (Child Class): The class that inherits from a superclass.
  • extends: Keyword used to establish inheritance between classes.
  • super: Keyword used to refer to the superclass's members or constructors.
  • Method Overriding: Redefining a superclass method in a subclass.
  • Polymorphism: The ability of a superclass reference to refer to a subclass object.
  • Abstract Classes: Classes that cannot be instantiated and may contain abstract methods.
  • Abstract Methods: Methods declared without implementation.
  • Interfaces: A collection of abstract methods and constants that a class can implement.
  • implements: Keyword used to implement an interface.

Example Code


// Example: Inheritance in Java
class Animal {
    protected String name;
    
    public Animal(String name) {
        this.name = name;
    }
    
    public void makeSound() {
        System.out.println("Some sound");
    }
}

class Dog extends Animal {
    public Dog(String name) {
        super(name);
    }
    
    @Override
    public void makeSound() {
        System.out.println("Woof!");
    }
}

class Cat extends Animal {
    public Cat(String name) {
        super(name);
    }
    
    @Override
    public void makeSound() {
        System.out.println("Meow!");
    }
}

public class InheritanceExample {
    public static void main(String[] args) {
        Animal dog = new Dog("Buddy");
        Animal cat = new Cat("Whiskers");
        
        dog.makeSound();
        cat.makeSound();
    }
}
              

Interactive Code Playground

Try out your own inheritance code here!

Output:
Note: This is a simulated environment. For actual Java code execution, please use an IDE or online Java compiler.

For actual code execution, we recommend using:

Multiple Choice Quiz

Question 1: Inheritance Basics

Which keyword is used to establish inheritance between classes in Java?

extends
implements
inherits
derives
The correct answer is A. The 'extends' keyword is used to create a subclass that inherits from a superclass in Java.
Question 2: Method Overriding

What is the purpose of the @Override annotation in Java?

To indicate that a method is being overridden from a superclass
To create a new method in the subclass
To prevent a method from being overridden
To make a method private
The correct answer is A. The @Override annotation indicates that a method is intended to override a method in a superclass, helping catch errors if the method signature doesn't match.
Question 3: Super Keyword

What is the purpose of the super keyword in Java?

To call the superclass constructor or methods
To create a new superclass object
To make a class a superclass
To prevent inheritance
The correct answer is A. The super keyword is used to call the superclass constructor or methods from within a subclass.

Practice Problems

  1. Create a class hierarchy for a banking system with Account as the superclass and CheckingAccount and SavingsAccount as subclasses.
  2. Implement an abstract Shape class with concrete subclasses Circle, Rectangle, and Triangle that calculate their respective areas.
  3. Design a class hierarchy for vehicles with appropriate inheritance relationships and method overriding.
  4. Create an interface Payable with a method calculatePay(), then implement it in Employee and Invoice classes.
  5. Implement a simple game with Character as a superclass and Player and Enemy as subclasses, with appropriate methods and attributes.

Tips for Success

  • Understand the "is-a" relationship: Inheritance should model an "is-a" relationship (e.g., a Car "is-a" Vehicle).
  • Use composition for "has-a" relationships: If one class "has-a" relationship with another, use composition instead of inheritance.
  • Keep the hierarchy shallow: Deep inheritance hierarchies can become difficult to understand and maintain.
  • Remember that constructors are not inherited: You need to explicitly call the superclass constructor using super().
  • Make use of @Override annotation: This helps catch errors at compile time when attempting to override methods.
  • Understand when to use abstract classes vs. interfaces: Abstract classes allow some implementation, while interfaces are pure contracts.
  • Use access modifiers appropriately: Protected members are accessible in subclasses, but private members are not inherited.