Skip to content
IRC-Coding IRC-Coding
Inheritance Polymorphism Liskov Substitution Principle Diamond Problem Base Classes

OOP Inheritance: Fundamentals, Polymorphism & Design

Master inheritance for code reuse in base classes. Learn is-a relationships, polymorphism, Liskov Substitution Principle, and the Diamond Problem.

S

schutzgeist

2 min read

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

  1. Base class (superclass): Common properties and behavior
  2. Subclass (subclass): Inherits and extends base class
  3. Method overriding: Customized behavior in subclasses
  4. Polymorphism: One interface, many implementations
  5. Dynamic binding: Method call resolved at runtime
  6. Abstract classes: Cannot be instantiated
  7. Interfaces: Pure contract definition without implementation
  8. 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

  1. 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.

  2. Explain the Liskov Substitution Principle! Subclasses must be replaceable by their base classes without the program behavior changing.

  3. 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.

  4. When should you use abstract classes vs interfaces? Use abstract classes for shared implementation, interfaces for pure contract definition.

Most Important Sources

  1. https://de.wikipedia.org/wiki/Vererbung_(Programmierung)
  2. https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html
  3. https://refactoring.guru/de/design-patterns

Keine Bücher für Kategorie "objektorientierte-programmierung" gefunden.

Back to Blog
Share:

Related Posts