INHERITANCE
Pattern Study Series: Technical Overview
Overview
Inheritance is a core pillar of Object-Oriented Programming (OOP). It allows a new class
(Subclass/Child) to inherit the attributes and methods of an existing class (Superclass/
Parent).
Primary Benefits
• Code Reusability: Define common logic once in the parent class.
• Extensibility: Child classes can add their own unique features.
• Method Overriding: Child classes can provide a specific implementation of a method
defined in the parent.
class Animal:
def speak(self):
print("Animal makes a sound")
class Dog(Animal): # Inherits from Animal
def speak(self): # Overriding
print("Bark!")
my_dog = Dog()
my_dog.speak() # Output: Bark!
Design Principle: Favor "Composition over Inheritance" if you only need parts of a
class's functionality. Use inheritance when there is a clear "is-a" relationship (e.g., a
Dog is an Animal).
Programming Concepts & Patterns Reference Guide • 2024