OOP Inheritance: Fundamentals, Implementation & Polymorphism
This article provides a conceptual overview of inheritance in object-oriented programming — including exam questions and key terminology.
In a Nutshell
Inheritance lets you define common properties and behavior in a base class, then reuse and extend them in subclasses. The goal is code reuse, polymorphism, and clear type relationships without duplicating code.
Core Definition
Inheritance establishes an is-a relationship between types, where a subclass inherits all public and protected members of a base class and can extend or override them.
It enables polymorphism, dynamic dispatch, and late method binding. At runtime, the call resolves to the actual object type rather than the reference type.
We distinguish implementation inheritance from interface inheritance, typically via interfaces. The Liskov Substitution Principle requires that subclasses behave like their base class without surprising clients.
Issues such as the fragile base class problem, the Diamond Problem, and tight coupling argue for composition over inheritance when you need code reuse without a genuine is-a relationship.
Exam-Relevant Key Points
- Is-a relationship between subclass and base class
- Polymorphism enables dynamic method calls at runtime
- Liskov Substitution Principle: Subclasses must be substitutable for their base classes
- Diamond Problem: Multiple inheritance can introduce ambiguity
- Implementation inheritance vs interface inheritance
- UML notation: Open arrowhead from subclass to superclass
- Access modifiers control extensibility and overridability
- Composition over inheritance for pure code reuse
Core Components
- Base class (Superclass): Defines common properties and behavior
- Subclass: Inherits and extends the base class
- Method overriding: Customized behavior in subclasses
- Polymorphism: One interface, many implementations
- Dynamic binding: Method call resolved at runtime
- Abstract classes: Cannot be instantiated
- Interfaces: Pure contract definition without implementation
- Final methods: Cannot be overridden
Practical Example
// Example: Inheritance in an animal hierarchy
abstract class Animal {
protected String name;
public Animal(String name) {
this.name = name;
}
public abstract void makeSound();
public void sleep() {
System.out.println(name + " is sleeping.");
}
}
class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override
public void makeSound() {
System.out.println(name + " barks: Woof!");
}
public void wagTail() {
System.out.println(name + " wags its tail.");
}
}
class Cat extends Animal {
public Cat(String name) {
super(name);
}
@Override
public void makeSound() {
System.out.println(name + " meows: Meow!");
}
}
// Polymorphism in action
Animal[] animals = {new Dog("Bella"), new Cat("Mimi")};
for (Animal animal : animals) {
animal.makeSound(); // Dynamic binding
animal.sleep();
}
Advantages and Disadvantages
Advantages
- Code reuse: Define shared behavior once in the base class
- Polymorphism: Treat different types uniformly
- Extensibility: Add new functionality through subclasses
- Organization: Clear hierarchies and relationships
Disadvantages
- Tight coupling: Subclasses become tightly bound to their base class
- Fragile base class: Changes can break all subclasses
- Diamond Problem: Multiple inheritance can cause conflicts
- Over-engineering: Using inheritance where composition fits better
Common Exam Questions
-
What’s the difference between inheritance and composition? Inheritance models an is-a relationship; composition models a has-a relationship. Prefer composition when you need only code reuse without a genuine is-a bond.
-
Explain the Liskov Substitution Principle. Subclasses must be substitutable for their base classes without altering the program’s correctness.
-
What is the Diamond Problem? In multiple inheritance, a class may inherit from two base classes that both inherit from a common ancestor, creating ambiguity about which inherited member to use.
-
When should you use abstract classes versus interfaces? Use abstract classes for shared implementation; use interfaces for pure contract definitions.
Key Resources
- https://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)
- https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
- https://refactoring.guru/design-patterns
Continue Your OOP Learning Path
The next article in the OOP learning path covers OOP Polymorphism: Fundamentals, Dynamic Binding & Dispatch — showing how polymorphism enables uniform calls to different implementations.
Recommended Reading: Object-Oriented Programming
Keine Bücher für Kategorie "object-oriented-programming" gefunden.



