OOP Inheritance: Fundamentals, Inheritance & Polymorphism
This article is a definition of terms for inheritance in object-oriented programming – including exam questions and tags.
In a Nutshell
Inheritance allows you to define common properties and behavior in a base class and reuse them in subclasses. The goal is code reuse, polymorphism, and clear type relationships without redundant code.
Compact Technical Description
Inheritance forms an is-a relationship between types, in which a subclass inherits all public and protected features of the base class and can extend or override them.
It enables polymorphism, dynamic binding, and late method selection. Dispatch occurs based on the actual object type at runtime.
A distinction is made between implementation inheritance and interface inheritance, such as via interfaces. The Liskov Substitution Principle requires that subclasses behave like their base class without surprising clients.
Problems such as fragile base class, diamond problem, and tight coupling advocate for composition over inheritance when only code reuse without a true is-a relationship is needed.
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 replaceable by base classes
- Diamond Problem: Multiple inheritance can lead to ambiguities
- Implementation inheritance vs interface inheritance
- UML representation: Open arrowhead from subclass to superclass
- Access modifiers control extensibility and overridability
- Composition over inheritance for pure code reuse
Core Components
- Base class (superclass): Common properties and behavior
- Subclass (subclass): Inherits and extends 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 animal hierarchy
abstract class Tier {
protected String name;
public Tier(String name) {
this.name = name;
}
public abstract void machGeraeusch();
public void schlafen() {
System.out.println(name + " schläft.");
}
}
class Hund extends Tier {
public Hund(String name) {
super(name);
}
@Override
public void machGeraeusch() {
System.out.println(name + " bellt: Wuff!");
}
public void wedleSchwanz() {
System.out.println(name + " wedelt mit dem Schwanz.");
}
}
class Katze extends Tier {
public Katze(String name) {
super(name);
}
@Override
public void machGeraeusch() {
System.out.println(name + " miaut: Miau!");
}
}
// Polymorphism in action
Tier[] tiere = {new Hund("Bello"), new Katze("Mimi")};
for (Tier tier : tiere) {
tier.machGeraeusch(); // Dynamic binding
tier.schlafen();
}
Advantages and Disadvantages
Advantages
- Code reuse: Define common behavior centrally
- Polymorphism: Uniform treatment of different types
- Extensibility: Add new functionality through subclasses
- Structuring: Clear hierarchies and relationships
Disadvantages
- Tight coupling: Subclasses tightly bound to base classes
- Fragile base class: Changes can break all subclasses
- Diamond Problem: Multiple inheritance can lead to conflicts
- Over-engineering: Inheritance where composition would be better
Common Exam Questions
-
What is the difference between inheritance and composition? Inheritance is an is-a relationship, composition is a has-a relationship. Prefer composition for pure code reuse.
-
Explain the Liskov Substitution Principle! Subclasses must be replaceable by their base classes without the program behavior changing.
-
What is the Diamond Problem? With multiple inheritance, a class can inherit from two base classes, which in turn inherit from a common base class.
-
When should you use abstract classes vs interfaces? Use abstract classes for shared implementation, interfaces for pure contract definition.
Most Important Sources
- https://de.wikipedia.org/wiki/Vererbung_(Programmierung)
- https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
- https://refactoring.guru/de/design-patterns
Recommended Reading: Object-Oriented Programming
Keine Bücher für Kategorie "objektorientierte-programmierung" gefunden.