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 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);
}
}
π 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
- Create a parent class
GameCharacterwith a methodmove(). - Create a child class
Wizardthat inheritsGameCharacter. - Add a unique method
castSpell()to theWizardclass. - In
main, create a Wizard and call bothmove()andcastSpell().
π 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!"