OOP Fundamentals
Inheritance
A mechanism where a new class inherits properties and behaviors from an existing class, enabling code reuse and establishing IS-A relationships.
12 min readEssential
1What is Inheritance?
Real-World Analogy
Family Traits
Children inherit traits from parents - eye color, height, etc. Similarly, a child class inherits attributes and methods from its parent class. The child can also have its own unique traits.
Vehicle Hierarchy
All vehicles have wheels and can move. Cars, bikes, and trucks are vehicles but each has special features. Car IS-A Vehicle. Bike IS-A Vehicle. They inherit common behavior but add their own.
Inheritance is an OOP mechanism where a child class (subclass) inherits attributes and methods from a parent class (superclass). It establishes an IS-A relationship and promotes code reuse.
IS-A Relationship Hierarchy
Animal
name, age, eat(), sleep()
extends (IS-A)
Dog
bark()
Cat
meow()
Bird
fly()
Dog IS-A Animal. Cat IS-A Animal. All inherit eat() and sleep().
2Types of Inheritance
Single Inheritance
One child extends one parent
Example: Dog extends Animal
Supported: Java, Python, C++
Multilevel Inheritance
Chain of inheritance (A → B → C)
Example: Puppy extends Dog extends Animal
Supported: Java, Python, C++
Hierarchical Inheritance
Multiple children extend one parent
Example: Dog, Cat, Bird all extend Animal
Supported: Java, Python, C++
Multiple Inheritance
One child extends multiple parents
Example: FlyingCar extends Car, Plane
Supported: Python, C++ (not Java)
Diamond Problem
Multiple inheritance can cause the Diamond Problem: if A extends B and C, and both B and C have a method foo(), which one does A inherit? Java avoids this by not allowing multiple class inheritance (but allows multiple interfaces).
3Implementation
Animal Hierarchy - Inheritance Example
// ========== Parent Class (Superclass) ==========
public class Animal {
protected String name;
protected int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {
System.out.println(name + " is eating");
}
public void sleep() {
System.out.println(name + " is sleeping");
}
public void displayInfo() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
// ========== Child Class (Subclass) ==========
public class Dog extends Animal {
private String breed;
public Dog(String name, int age, String breed) {
super(name, age); // Call parent constructor
this.breed = breed;
}
// Dog-specific method
public void bark() {
System.out.println(name + " says: Woof! Woof!");
}
// Override parent method
@Override
public void displayInfo() {
super.displayInfo(); // Call parent method
System.out.println("Breed: " + breed);
}
}
// ========== Another Child Class ==========
public class Cat extends Animal {
private boolean isIndoor;
public Cat(String name, int age, boolean isIndoor) {
super(name, age);
this.isIndoor = isIndoor;
}
public void meow() {
System.out.println(name + " says: Meow!");
}
public void scratch() {
System.out.println(name + " is scratching");
}
}
// ========== Usage ==========
public class Main {
public static void main(String[] args) {
Dog dog = new Dog("Buddy", 3, "Golden Retriever");
dog.eat(); // Inherited from Animal
dog.sleep(); // Inherited from Animal
dog.bark(); // Dog-specific
dog.displayInfo(); // Overridden method
System.out.println();
Cat cat = new Cat("Whiskers", 2, true);
cat.eat(); // Inherited from Animal
cat.meow(); // Cat-specific
// Polymorphism - Animal reference to Dog object
Animal animal = new Dog("Rex", 5, "German Shepherd");
animal.eat(); // Works - inherited method
// animal.bark(); // Error! Animal reference cannot see Dog methods
}
}Output:
Buddy is eating Buddy is sleeping Buddy says: Woof! Woof! Name: Buddy, Age: 3 Breed: Golden Retriever Whiskers is eating Whiskers says: Meow!
4Key Inheritance Concepts
super / base
Keyword to access parent class constructor and methods
super(name, age) calls parent constructor@Override
Annotation/keyword indicating a method overrides parent's method
Child's displayInfo() replaces parent'sprotected
Access modifier - visible to child classes but not outside
protected String name; - accessible in Dogfinal / sealed
Prevents a class from being extended or method from being overridden
final class String - cannot extend String5When to Use (and When Not To)
✅ Use Inheritance When
- Clear IS-A relationship exists
- Child is a specialized version of parent
- You want to reuse parent behavior
- Polymorphism is needed
- Behavior is truly shared, not just similar
❌ Avoid Inheritance When
- Only want code reuse (use composition)
- Relationship is HAS-A, not IS-A
- Hierarchy would be deep (>3 levels)
- Child needs to ignore parent methods
- You are inheriting just for convenience
Composition Over Inheritance
Favor composition over inheritance. Instead of Dog extending Animal, Dog can HAVE an AnimalBehavior. This is more flexible - you can change behavior at runtime. Use inheritance for true IS-A relationships, composition for HAS-A.
6Interview Questions
What is inheritance and why do we use it?
Inheritance allows a child class to inherit properties and methods from a parent class. We use it for: 1) Code reuse - write once, use in multiple classes, 2) Establishing IS-A relationships, 3) Enabling polymorphism - treat child objects as parent type, 4) Creating type hierarchies.
What is the difference between method overriding and overloading?
Overriding: Child class provides its own implementation of a parent method (same signature, different class). Overloading: Same class has multiple methods with the same name but different parameters. Overriding is runtime polymorphism; overloading is compile-time.
Why does Java not support multiple inheritance of classes?
To avoid the Diamond Problem. If class A extends B and C, and both B and C have method foo(), which one should A inherit? This ambiguity causes issues. Java solves it by allowing single class inheritance but multiple interface implementation (interfaces can have default methods since Java 8).
7Key Takeaways
1Inheritance establishes IS-A relationship between classes.
2Child class inherits attributes and methods from parent class.
3Use
super to call parent constructor and methods.4
@Override indicates a method replaces parent implementation.5Favor composition over inheritance when possible.
6Java allows single class inheritance but multiple interface implementation.