Inheritance in Java
- coding z2m
- Jun 30
- 2 min read
🎯 Goal of Inheritance
Inheritance enables code reuse and creates a hierarchical relationship between classes.
It allows a subclass to inherit fields and methods from a superclass and also define its own behavior.
🏢 Real-World Scenario: Employee Management System
Consider a company with:
Common attributes for all employees (name, ID, salary)
Specific behaviors or fields for different roles:
Manager has a team size
Engineer has a specialization
Intern has a duration
This is a perfect use case for inheritance.
🔹 Java Code Example
1️⃣ Superclass: Employee public class Employee {
protected String name;
protected int employeeId;
protected double salary;
public Employee(String name, int employeeId, double salary) {
this.name = name;
this.employeeId = employeeId;
this.salary = salary;
}
public void displayDetails() {
System.out.println("Name: " + name);
System.out.println("ID: " + employeeId);
System.out.println("Salary: ₹" + salary);
}
} 2️⃣ Subclass: Manager public class Manager extends Employee {
private int teamSize;
public Manager(String name, int employeeId, double salary, int teamSize) {
super(name, employeeId, salary); // 🪜 Call to parent constructor
this.teamSize = teamSize;
}
@Override
public void displayDetails() {
super.displayDetails(); // Reuse base info
System.out.println("Team Size: " + teamSize);
}
} 3️⃣ Subclass: Engineer public class Engineer extends Employee {
private String specialization;
public Engineer(String name, int employeeId, double salary, String specialization) {
super(name, employeeId, salary);
this.specialization = specialization;
}
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Specialization: " + specialization);
}
} 4️⃣ Subclass: Intern public class Intern extends Employee {
private int internshipDurationMonths;
public Intern(String name, int employeeId, double salary, int duration) {
super(name, employeeId, salary);
this.internshipDurationMonths = duration;
}
@Override
public void displayDetails() {
super.displayDetails();
System.out.println("Internship Duration: " + internshipDurationMonths + " months");
}
} 🔄 Usage in Main public class Main {
public static void main(String[] args) {
Manager m = new Manager("Alice", 1001, 90000.0, 5);
Engineer e = new Engineer("Bob", 1002, 75000.0, "Backend");
Intern i = new Intern("Charlie", 1003, 15000.0, 6);
m.displayDetails();
System.out.println("------------------");
e.displayDetails();
System.out.println("------------------");
i.displayDetails();
}
}
🧠 Key Concepts Demonstrated
Concept | Description |
super() | Calls constructor of the parent class |
@Override | Redefines inherited method to add subclass-specific behavior |
protected fields | Allow access in subclasses |
Code reuse | All subclasses reuse name, salary, and displayDetails() from Employee |
💬 Interview-Ready Explanation
"In my employee management model, I used inheritance to avoid repeating common attributes and behavior. I created a superclass Employee and extended it with subclasses like Manager and Engineer. Each subclass overrides displayDetails() to show role-specific data, demonstrating polymorphism as well."
🧪 Student Practice Challenge
Create a class Vehicle with properties like make, model, price.Then extend it to create:
ElectricCar (with battery capacity),
Bike (with gear count),
Truck (with payload limit).
Override a method displayInfo() to include subclass details.
Comments