devnotes.

Types of Inheritance

Different OOP languages support different shapes of inheritance hierarchy:

  • Single inheritance — a class extends exactly one parent class. Supported by Java, C#, PHP, Python (for classes, though Python allows multiple).
class Employee extends Person { ... }
  • Multilevel inheritance — a chain: C extends B, B extends A. C inherits everything from both B and A.
class Animal { ... }
class Mammal extends Animal { ... }
class Dog extends Mammal { ... } // Dog inherits from both Mammal and Animal
  • Hierarchical inheritance — multiple subclasses inherit from the same single parent.
class Shape { ... }
class Circle extends Shape { ... }
class Square extends Shape { ... }
class Triangle extends Shape { ... }
  • Multiple inheritance — a class inherits directly from more than one parent class. Supported natively by C++ and Python, but not by Java, C#, or PHP for classes — these languages instead let a class implement multiple interfaces (covered in a later chapter) to get similar benefits without the classic 'diamond problem' (ambiguity when two parent classes define a method with the same name).

  • Hybrid inheritance — a combination of the above patterns in one hierarchy.

A guiding rule of thumb: favor shallow, single-inheritance hierarchies where possible. A subclass should only extend a parent when the 'is-a' relationship is genuinely true and stable — 'a Square is-a Shape' holds up, but modeling 'a Car is-a Engine' (rather than 'a Car has-a Engine') is a common beginner mistake that composition (next chapter) solves more cleanly.

Types of Inheritance in OOP | DevNotes