OOP · The Four Pillars
Inheritance
Hides internal complexity.
- Hides internal complexity.
- Improves security.
- Makes code flexible.
- Promotes loose coupling.
- Supports scalable application development.
Real-World Problem
Imagine you are developing an Employee Management System for a company. The company has different types of employees, such as:
- 👨 💻 Developer
- 👩 💼 Manager
- 🧪 Tester
Each employee has common information, including:
- Employee ID
- Name
- Salary

- Department
If you create a separate class for each employee type, you will end up writing the same fields and methods repeatedly. This leads to code duplication, making the application harder to maintain and update.
Problems Without Inheritance The same fields are duplicated in every class, making the code difficult to maintain.

- Duplicate code across multiple classes.
- Increased development time.

- Difficult to maintain and update.
- Higher chances of introducing errors.
- Poor code reusability.
Solution: Inheritance Instead of defining the same members in every class, move the common fields and methods into a single parent class. Other classes can then inherit these common members from the parent class and define only their own unique features.
This approach reduces code duplication, improves code reusability, and makes the application easier to maintain. What is Inheritance?
Inheritance is an Object-Oriented Programming (OOP) concept that allows one class to acquire the properties and behaviors of another class. The class being inherited from is called the Parent Class (Superclass), and the class that inherits those members is called the Child Class (Subclass).
Inheritance is implemented in Java using the extends keyword.
Advantages of Inheritance
- Code Reusability – Reuse common code from the parent class.

A subclass reuses and extends a parent's state and behavior via extends.
class Animal {
String name;
void eat() { System.out.println(name + " is eating"); }
}
// Dog IS-A Animal — inherits eat() and name, adds its own behavior
class Dog extends Animal {
void bark() { System.out.println(name + " says woof"); }
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
d.name = "Rex";
d.eat(); // inherited
d.bark(); // own
}
}