Skip to content

Java Inheritance (IS-A Relationship) πŸš€

Mentor's Note: Inheritance is about Efficiency and Classification. Why build a new kingdom from scratch if your parents already have one? In programming, if you can say "X is a type of Y", you use Inheritance to build on existing code! πŸ’‘


🌟 The Scenario: The Royal Family πŸ‘‘

Imagine you are part of a royal family.

  • The Superclass (Parent): The King has a Crown πŸ‘‘ and a Kingdom 🏰.
  • The Subclass (Child): The Prince Inherits the Crown and the Kingdom automatically.
  • The Extension: The Prince also buys a Fast Horse 🐎. He has everything the King has, PLUS his own new features. βœ…

🎨 Visual Logic: The Hierarchy

classDiagram
    Animal <|-- Dog
    Animal <|-- Cat

    Vehicle <|-- Car
    Vehicle <|-- Bike

    Superhero <|-- Superman
    Superhero <|-- Batman

πŸ“– Concept Explanation

1. Subclass vs. Superclass

  • Superclass (Parent): The class being inherited from.
  • Subclass (Child): The class that inherits from another class.

2. The extends Keyword

In Java, we use the extends keyword to perform inheritance.

3. The super Keyword 🦸

The super keyword is like a "Hotline" to the parent. It is used to: - Call the parent's constructor. - Access the parent's methods if they were overridden.


πŸ’» Implementation: The Multi-Scenario Lab

class Animal {
    void breathe() { System.out.println("Breathing... πŸ’¨"); }
    void move() { System.out.println("Moving... 🐾"); }
}

class Dog extends Animal {
    void bark() { System.out.println("Woof! Woof! πŸ•"); }
}
class Vehicle {
    protected String brand = "Ford"; 

    public void honk() { 
        System.out.println("Tuut, tuut! πŸ”Š");
    }
}

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

    public static void main(String[] args) {
        Car myCar = new Car();
        myCar.honk(); // Using inherited method
        System.out.println(myCar.brand + " " + myCar.modelName);
    }
}
class Superhero {
    void fight() { System.out.println("Fighting evil! πŸ‘Š"); }
}

class Spiderman extends Superhero {
    void shootWeb() { System.out.println("Thwip! πŸ•ΈοΈ"); }
}

πŸ“Š Sample Dry Run (Vehicle Example)

Step Action Computer's Logic Result
1 new Car() Reserve memory for brand (Parent) and modelName (Child) Object created πŸ—οΈ
2 myCar.honk() Check Car class? (No) -> Check Vehicle class? (Yes) Prints "Tuut!" πŸ”Š
3 myCar.brand Directly access inherited protected variable Prints "Ford" βœ…

πŸ“ˆ Technical Analysis & Constraints

1. The extends Keyword

This is the magic link. In Java, a class can have only one direct parent (Single Inheritance).

2. The super Keyword 🦸

The super keyword is used to: - Call the parent's constructor: super(); - Access parent methods: super.methodName();

3. Access Control πŸ”‘

  • Protected: Members are visible to subclasses even if they are in different packages.
  • Private: These are NEVER inherited. They stay hidden in the parent's class.

4. The Diamond Problem πŸ’Ž

Java does not allow inheriting from two classes at once (e.g., class Child extends Mom, Dad) to avoid confusion when both parents have the same method name.


🎯 Practice Lab πŸ§ͺ

Task: The Game Character

  1. Create a parent class GameCharacter with a method move().
  2. Create a child class Wizard that inherits GameCharacter.
  3. Add a unique method castSpell() to the Wizard class.
  4. In main, create a Wizard and call both move() and castSpell().

πŸ“Š Summary Table

Relationship Logic Example
Inheritance IS-A A Doctor is a Person
Subclass The Specialist The Surgeon
Superclass The General The Doctor

πŸ’‘ Interview Tip πŸ‘”

"Interviewers love asking: 'Does a subclass inherit the constructor of its superclass?' Answer: NO. Constructors are not inherited, but the subclass must call the parent constructor using super() as its first action."


πŸ’‘ Pro Tip: "Stand on the shoulders of giantsβ€”use inheritance to build on what already exists!"


πŸ“ˆ Learning Path