1. The Hook (The "Byte-Sized" Intro)
In a Nutshell: In Java, inheritance is implemented using the extends keyword. When a class extends another, it automatically gains all non-private members of the parent class. It's the syntax that turns the concept of inheritance into working code.
Think of Google Drive folders. When you create a subfolder inside "Work," it automatically extends access permissions from the parent folder. The subfolder inherits the sharing settings but can also add its own unique files.
2. Conceptual Clarity (The "Simple" Tier)
💡 The Analogy: The Family Recipe Book
Imagine a family recipe book passed down through generations:
- Grandma's Book (Parent): Has 50 classic recipes.
- Mom's Book (Child extends Grandma): Inherits all 50 recipes PLUS adds 20 modern twists.
- Your Book (Grandchild extends Mom): Inherits 70 recipes PLUS adds 10 fusion dishes.
Each generation extends the previous one without rewriting everything!
Hand-Drawn Logic Map
3. Technical Mastery (The "Deep Dive")
Formal Definition
The extends keyword establishes an inheritance relationship between two classes. Syntax:
class ChildClass extends ParentClass {
// Child members
}What Gets Inherited:
- ✅ Public and protected fields/methods
- ✅ Package-private members (if in same package)
- ❌ Private members (not accessible, but exist in memory)
- ❌ Constructors (not inherited, but can be called via
super())
The "Why" Paragraph
Why single inheritance only? Languages like C++ allow multiple inheritance, but it creates the Diamond Problem: if C extends both A and B, and both have a method foo(), which one does C inherit? Java chose simplicity and clarity over power—one parent per class, but unlimited interface implementation.
Visual Architecture: Types of Inheritance
4. Interactive & Applied Code
The "Perfect" Code Block
// Parent Class
class Employee {
String name;
double salary;
void work() {
System.out.println(name + " is working...");
}
void displaySalary() {
System.out.println("Salary: $" + salary);
}
}
// Single Inheritance
class Manager extends Employee {
int teamSize;
void conductMeeting() {
System.out.println(name + " is conducting a meeting with " + teamSize + " people");
}
}
// Multilevel Inheritance
class SeniorManager extends Manager {
String department;
void planStrategy() {
System.out.println(name + " is planning strategy for " + department);
}
}
// Hierarchical Inheritance
class Developer extends Employee {
String programmingLanguage;
void code() {
System.out.println(name + " is coding in " + programmingLanguage);
}
}
public class Main {
public static void main(String[] args) {
// Manager has: name, salary, work(), displaySalary(), teamSize, conductMeeting()
Manager mgr = new Manager();
mgr.name = "Alice";
mgr.salary = 80000;
mgr.teamSize = 10;
mgr.work(); // Inherited
mgr.conductMeeting(); // Own method
// SeniorManager has ALL Manager + Employee members
SeniorManager seniorMgr = new SeniorManager();
seniorMgr.name = "Bob";
seniorMgr.teamSize = 50;
seniorMgr.department = "Engineering";
seniorMgr.planStrategy(); // Own method
seniorMgr.work(); // From Employee (grandparent!)
}
}The "Anti-Pattern" Example
❌ Trying Multiple Inheritance
class C extends A, B { } // ❌ COMPILE ERROR! Java doesn't support thisSolution: Use interfaces for multiple "contracts":
class C extends A implements B, D { } // ✅ Correct!5. The Comparison & Decision Layer
Versus Table: Types of Inheritance
| Type | Structure | Example |
|---|---|---|
| Single | Child extends one parent | Dog extends Animal |
| Multilevel | Chain (grandparent-parent-child) | Puppy extends Dog extends Animal |
| Hierarchical | One parent, many children | Dog, Cat, Bird all extend Animal |
6. The "Interview Corner" (The Edge)
The "Killer" Interview Question: "What is inherited and what is not?" Answer:
- Inherited: Public/protected methods & fields
- NOT Inherited: Constructors, private members
- Special Case: Private members exist in memory but aren't directly accessible in child class
JVM Note
Memory Layout: When you create a Manager object, the JVM allocates space for both Employee fields and Manager fields in a single heap block. There's no "two separate objects"—it's one object with a combined structure.
Pro-Tip: Use @Override annotation even when extending (not just overriding). Modern IDEs suggest this to make intent clear and catch signature mismatches!