Inheritance

🧠 What is Inheritance?

Inheritance is a core object-oriented programming principle that allows one class (called a subclass or child class) to inherit the fields and methods of another class (called a superclass or parent class).

It enables the reuse of code and supports hierarchical classification.


πŸ” Real-world Analogy:

Imagine a Vehicle class. A Car, Bike, and Truck are all vehicles β€” they share common properties like speed, engine, and methods like start() or stop().

Instead of duplicating code in each class, we define these common features once in the Vehicle class and have Car, Bike, and Truck inherit from it.


πŸ”§ Java Syntax Example

// Parent Class
class Vehicle {
    int speed = 50;

    void start() {
        System.out.println("Vehicle is starting...");
    }
}

// Child Class
class Car extends Vehicle {
    void honk() {
        System.out.println("Car horn: Beep beep!");
    }
}

// Main Class
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.start();    // Inherited method
        myCar.honk();     // Child method
        System.out.println("Speed: " + myCar.speed);  // Inherited field
    }
}

🎯 Why Use Inheritance?

Inheritance isn’t just about reusing code β€” it plays a fundamental role in software architecture.

βœ… Code Reusability

You avoid writing redundant code by placing shared logic in the parent class.

βœ… Extensibility

New classes can be built on existing ones, following the Open/Closed Principle (open for extension, closed for modification).

βœ… Polymorphism

Inheritance allows subclasses to override parent methods, enabling dynamic behavior at runtime.

βœ… Hierarchical Modeling

You can represent real-world relationships in a class hierarchy, which enhances code readability and maintainability.


πŸ”₯ Key Rules of Inheritance in Java

RuleDescription
extends keywordUsed to inherit a class
Single inheritanceJava only supports single class inheritance (one parent)
ConstructorsNot inherited, but super() can call parent constructor
Private membersNot directly accessible in child classes
Static membersInherited but not overridden

❗ Common Pitfalls

  • Overusing inheritance can lead to rigid and tightly coupled systems.
  • Prefer composition over inheritance when β€œhas-a” makes more sense than β€œis-a”.

🏠 Homework for Students

1. Create a class hierarchy where:

  • Employee is the base class with name, id, and a method work().
  • Manager extends Employee and adds a method conductMeeting().
  • Developer extends Employee and adds a method writeCode().

2. Create objects of both child classes and demonstrate:

  • Inherited methods and fields.
  • Child-specific behavior.
  • Print outputs showing inheritance in action.

Would you like to proceed with Access Modifiers next, or go into Method Overriding inside inheritance (deeper dive)?