Java Inheritance

Java Inheritance (Subclasses and Superclasses)

In Java, it is possible to inherit attributes and methods from one class to another. This is one of the core principles of Object-Oriented Programming.

We group the "inheritance concept" into two categories:

To inherit from a class, use the extends keyword.

💡 The "IS-A" Relationship: Inheritance is strictly used to model an "IS-A" relationship.

  • A Dog IS-A Animal. (Valid inheritance)
  • A Car IS-A Vehicle. (Valid inheritance)
  • A Car IS-A Engine. (Invalid! A Car HAS-AN engine. This would be composition, not inheritance).

Why Use Inheritance?

Deep Concept: The Cosmic Object Class

Did you know that every single class in Java inherits from something? Even if you don't use the extends keyword, Java automatically makes your class inherit from the built-in java.lang.Object class behind the scenes.

This is why every object you create automatically has methods like .toString(), .equals(), and .hashCode()—they inherited them from the ultimate cosmic parent class, Object!


Inheritance Example

In the example below, the Car class (subclass) inherits the attributes and methods from the Vehicle class (superclass).

Inheritance Example

class Vehicle {
  protected String brand = "Ford"; // Vehicle attribute
  public void honk() {             // Vehicle method
    System.out.println("Tuut, tuut!");
  }
}

class Car extends Vehicle { private String modelName = "Mustang"; // Car attribute

public static void main(String[] args) { // Create a myCar object Car myCar = new Car(); // Call the honk() method (from the Vehicle class) on the myCar object myCar.honk(); // Display the value of the brand attribute (from the Vehicle class) // and the value of the modelName from the Car class System.out.println(myCar.brand + " " + myCar.modelName); } }

Did you notice the protected modifier in Vehicle?

We set the brand attribute in Vehicle to a protected access modifier. If it were set to private, the Car class would not be able to access it. The protected modifier makes the attribute visible to the subclass.


The final Keyword

If you don't want other classes to inherit from a class, use the final keyword. A final class cannot be extended.

final class Vehicle {
  // ...
}

// class Car extends Vehicle { } // This would cause a compilation error


Deep Dive: The super Keyword

When you inherit from a class, you often need to refer back to the parent class to access its constructor or its specific implementation of a method. This is where the super keyword comes in.

It has two main uses:

  1. super(): Calls the parent class's constructor. This must be the very first line inside the child class's constructor.
  2. super.methodName(): Calls a method from the parent class, even if the child class has overridden it.

Using the `super` Keyword

class Animal {
  String name;
  public Animal(String name) {
    this.name = name;
    System.out.println("Animal constructor called.");
  }
  public void eat() {
    System.out.println(name + " is eating.");
  }
}

class Dog extends Animal { public Dog(String name) { // 1. Call the parent's constructor super(name); System.out.println("Dog constructor called."); }

// Overriding the eat method @Override public void eat() { // 2. Call the parent's eat method first super.eat(); System.out.println("The dog finishes the food quickly."); } }

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


Why No Multiple Inheritance? (The Diamond Problem)

Unlike languages like C++, Java does not allow a class to inherit from more than one class using the extends keyword (e.g., class Child extends Parent1, Parent2 is illegal).

Why? To avoid the Diamond Problem. If Parent1 and Parent2 both have a method called start(), and the Child class tries to call start(), Java wouldn't know which parent's method to execute! To prevent this dangerous ambiguity, Java restricts classes to single inheritance.

(Note: Java bypasses this limitation using Interfaces, which we will cover in a later chapter).


Advanced Notes: Inheritance Design


Exercise

?

Which keyword is used to inherit from a class in Java?