Skip to content
IRC-Coding IRC-Coding
Inheritance OOP Base Class Subclass Overriding Polymorphism Liskov Composition

Inheritance in OOP Fundamentals Explained

Learn inheritance basics: define shared properties in base classes, is-a relationships, method overriding, polymorphism, Liskov principle, and composition over inheritance.

S

schutzgeist

2 min read

Inheritance OOP Basics – Base Class, Subclass, Overriding, Polymorphism, Liskov

This article is a concept explanation on inheritance in OOP – 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 establishes an is-a relationship between types, where 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 speak in favor of composition over inheritance when only code reuse without a true is-a relationship is needed.

Exam-Relevant Key Points

  • is-a relationship, use correctly, not for has-a
  • Overriding, override, enables polymorphic behavior, signature must remain compatible
  • Visibility, public, protected, private, affects inheritability and access
  • IHK relevant, distinguish between implementation inheritance and interface inheritance
  • Composition over inheritance, lower coupling, better interchangeability in practice
  • Adhere to LSP, do not tighten preconditions, do not weaken postconditions, maintain invariants
  • Cost-effectiveness, fewer duplicates, but risk of expensive refactoring with incorrect hierarchy
  • Documentation obligation, contracts, side effects, overriding rules, extension points to be documented

Core Components

  1. Base class, superclass with common attributes and methods
  2. Subclass, subclass that inherits, extends, overrides
  3. Overriding, override with dynamic dispatch
  4. Overloading, overload, same method, different parameters, no inheritance needed
  5. Abstract class, common basis with partially implemented behavior
  6. Interface, pure contract interface for multiple inheritance of types
  7. Finality, final sealing against overriding, inheritance
  8. Constructor chaining, super call, initialization order
  9. Access rights, protected for extenders, private for strict encapsulation
  10. Test procedures, substitution and contract tests, polymorphism tests

Practical Example

// Example, Java-like syntax with inheritance and polymorphism
abstract class Shape {
public abstract double area()
}

class Rectangle extends Shape {
private double w, h
public Rectangle(double w, double h) {
if (w <= 0 || h <= 0) throw new IllegalArgumentException("positive dimensions")
this.w = w
this.h = h
}
@Override
public double area() {
return w * h
}
}

class Circle extends Shape {
private double r
public Circle(double r) {
if (r <= 0) throw new IllegalArgumentException("positive radius")
this.r = r
}
@Override
public double area() {
return Math.PI * r * r
}
}

// Polymorphic usage
Shape s1 = new Rectangle(3, 4)
Shape s2 = new Circle(2)
double sum = s1.area() + s2.area()

Explanation: Shape defines the area contract, subclasses implement it specifically, calls are made polymorphically.

Advantages and Disadvantages

Advantages

  • Code reuse, polymorphism
  • Clear contract through common interface
  • Reduction of redundant implementations, uniform API for clients

Disadvantages

  • Tight coupling to base class decisions
  • Fragile hierarchies, difficult refactoring
  • Diamond problem with multiple inheritance, possible violation of encapsulation

Typical Exam Questions (with Short Answer)

  1. Is inheritance appropriate? When a true is-a relationship exists, the subclass can be used anywhere the base class is expected.

  2. Overriding vs. Overloading? Overriding replaces inherited behavior of the same signature in the subclass, overloading defines multiple methods of the same name with different parameters.

  3. Polymorphism in the context of inheritance? Reference type can hold objects of base class or subclasses, method calls bind at runtime to the actual object type.

  4. Why composition over inheritance? Composition reduces coupling, avoids rigid hierarchies, allows interchangeable implementations, better for code reuse without a true is-a relationship.

  5. Role of protected? protected allows subclasses to access, but can weaken encapsulation, use sparingly, prefer private with accessor methods.

  6. Liskov Substitution Principle requires? Do not tighten preconditions, do not weaken postconditions, maintain invariants of the base class, otherwise substitutability breaks.

  7. Explain the diamond problem? Multiple inheritance of two base classes with a common ancestor leads to ambiguity; some languages solve this with virtual inheritance.

  8. How to test substitutability? Contract tests against the base classes, define interface specifications, run the same tests against all subclass instances.

Most Important Sources

  1. https://docs.oracle.com/javase/specs/
  2. https://de.wikipedia.org/wiki/Vererbung_(Programmierung)
  3. https://martinfowler.com/articles/inheritance.html
Back to Blog
Share:

Related Posts