HOME HTML EDITOR C JAVA PHP

Java Inheritance: Architecting Hierarchies

Inheritance is the pillar of Java that turns "Code Reusability" into reality. It is a mechanism where a new class (Subclass) acquires the properties and behaviors (fields and methods) of an existing class (Superclass). This allows developers to build upon existing code without reinventing the wheel.

1. The "IS-A" Relationship

Inheritance always operates on an IS-A relationship. For example, "A Car IS-A Vehicle" or "A Surgeon IS-A Doctor." If this logical link exists, inheritance is likely the correct design choice.

  • Super Class (Parent): The class whose features are inherited.
  • Sub Class (Child): The class that inherits those features and can also add its own.

In Java, we use the extends keyword to establish this link.

2. Types of Inheritance in Java

Java supports several hierarchical structures, but with a specific architectural constraint regarding classes:

Single Inheritance

A subclass inherits from one single superclass. (Class B extends A)

Multilevel Inheritance

A derived class is inherited by another class. (C extends B, and B extends A)

Hierarchical Inheritance

Multiple subclasses inherit from one single parent. (B and C both extend A)

Critical Note: Java does not support Multiple Inheritance with classes (one child having two parents) to avoid the "Diamond Problem." This ambiguity is resolved in Java through the use of Interfaces.

3. The 'super' Keyword: Navigating the Parent

When using inheritance, the child class often needs to interact with the parent class. The super keyword is a reference variable used to refer to immediate parent class objects.

Primary Applications of super:

  1. To Invoke Constructors: super(); calls the parent constructor (must be the first line).
  2. To Invoke Methods: Used if the child has overridden a parent method but still needs the original version.
  3. To Access Fields: To distinguish between parent and child variables if they share the same name.

4. Method Overriding: Runtime Polymorphism

If a subclass provides a specific implementation for a method that is already provided by its parent class, it is known as Method Overriding.

Rules for Overriding: The method must have the same name, same return type, and same parameters as in the parent. Using the @Override annotation is highly recommended to prevent syntax errors during compilation.

5. Mastery Code Example: The Vehicle Hierarchy

The following example demonstrates how the extends and super keywords function within a real-world scenario.

class Vehicle {
  String brand;

  Vehicle(String brand) { // Parent Constructor
    this.brand = brand;
  }

  void honk() {
    System.out.println("Beep! Beep!");
  }
}

class Car extends Vehicle {
  Car(String brand) {
    super(brand); // Explicit call to Parent Constructor
  }

  @Override
  void honk() {
    System.out.println(brand + " says: HONK! HONK!");
  }
}

6. Memory Management in Inheritance

When you instantiate a child class (e.g., new Car()), the JVM handles memory in a specific sequence:

Concept Execution Reality
Constructor Chain The parent constructor is executed first, followed by the child's constructor.
Instance Variables One single object is created in the Heap, but it contains all fields from both the parent and the child.

7. Preventing Inheritance with 'final'

Sometimes you want to restrict inheritance for security or design reasons. The final keyword is your primary tool:

8. Interview Preparation: High-Level Q&A

Q: Why does Java use 'extends' instead of 'inherits'?
A: The keyword extends implies that the subclass is adding more functionality to the parent, thus "extending" its capabilities, which is a more accurate description than simply inheriting.

Q: Can a subclass constructor be private?
A: Yes, but then that subclass cannot be instantiated from outside, and no other class can inherit from it (unless it’s an inner class).

Q: Is every class in Java inherited from something?
A: Yes! Every class in Java implicitly inherits from the Object class (java.lang.Object) if no other parent is specified.

The Final Verdict

Inheritance is not just about reducing the amount of code you write; it is about creating a logical, scalable architecture. By following the "Don't Repeat Yourself" (DRY) principle, you ensure that your Java applications are easier to maintain, debug, and expand over time.

Next: Master Java Polymorphism →