01 / 21

Inheritance &
Method Overriding

Learn how Python classes can inherit attributes and methods from parent classes, and how child classes can override behavior to create specialized objects.

Python

What is Inheritance?

Inheritance is a fundamental concept in object-oriented programming where a new class (called the child or subclass) is created from an existing class (called the parent or superclass). The child class automatically receives all the attributes and methods of the parent.

Why Use Inheritance?

Code Reuse
Write common logic once in the parent class. All children get it for free.
Organization
Group related classes into hierarchies that mirror real-world relationships.
Extensibility
Add or modify behavior in child classes without changing the parent.

The Class Hierarchy Visualized

Animal name, speak() Dog speak() → "Woof!" Cat speak() → "Meow!" inherits inherits Both Dog and Cat override speak()

The parent class defines the shared structure. Each child class inherits it and can customize specific behavior.

The Inheritance Syntax

class Parent: def greet(self): return "Hello from Parent" class Child(Parent): # ← inherits from Parent pass c = Child() print(c.greet()) # "Hello from Parent"

What is Method Overriding?

Method overriding occurs when a child class defines a method with the same name as one in its parent class. The child's version replaces the parent's version when called on a child object.

class Animal: def speak(self): return "Some generic sound" class Dog(Animal): def speak(self): # ← overrides Animal.speak() return "Woof!" d = Dog() print(d.speak()) # "Woof!" (not "Some generic sound")

Inheritance vs. Overriding

Inheritance
The child class receives all methods and attributes from the parent automatically, without rewriting anything.
Method Overriding
The child class redefines a specific method from the parent, providing its own specialized implementation.

Calling the Parent with super()

Sometimes you want to extend the parent's method rather than fully replace it. The built-in super() function lets you call the parent's version from inside the child's override.

class Animal: def __init__(self, name): self.name = name class Dog(Animal): def __init__(self, name, breed): super().__init__(name) # ← call parent's __init__ self.breed = breed d = Dog("Rex", "Labrador") print(d.name, d.breed) # Rex Labrador

How to Apply Inheritance

1
Identify shared behavior. Find the attributes and methods that multiple classes have in common.
2
Create a parent class. Write a class containing the shared logic.
3
Define child classes. Put the parent name in parentheses: class Child(Parent):
4
Override where needed. Redefine any method in the child that needs different behavior.
5
Use super() to extend. Call super().method() when you want the parent logic plus extra child logic.

Example 1: Shape Hierarchy

class Shape: def area(self): return 0 def describe(self): return f"Area: {self.area()}" class Rectangle(Shape): def __init__(self, w, h): self.w = w self.h = h def area(self): # override return self.w * self.h r = Rectangle(5, 3) print(r.describe()) # "Area: 15"

Example 2: Extending with super()

class Vehicle: def __init__(self, make, year): self.make = make self.year = year def info(self): return f"{self.year} {self.make}" class ElectricCar(Vehicle): def __init__(self, make, year, battery_kwh): super().__init__(make, year) self.battery_kwh = battery_kwh def info(self): # override + extend return super().info() + f" ({self.battery_kwh} kWh)" e = ElectricCar("Tesla", 2024, 75) print(e.info()) # "2024 Tesla (75 kWh)"

Example 3: Multi-Level Inheritance

class Animal: def speak(self): return "..." class Dog(Animal): def speak(self): return "Woof!" class Puppy(Dog): # inherits from Dog (which inherits from Animal) def speak(self): return super().speak() + " Yip!" print(Puppy().speak()) # "Woof! Yip!"
Animal Dog Puppy Each level can override and extend via super() Puppy → Dog → Animal (Method Resolution Order)

Checking Types with isinstance()

d = Dog() p = Puppy() print(isinstance(d, Animal)) # True print(isinstance(p, Dog)) # True print(isinstance(p, Animal)) # True print(isinstance(d, Puppy)) # False

The Power of Polymorphism

Because child classes share the same method names as their parents, you can write functions that work with any object in the hierarchy. Python automatically calls the correct overridden version.

def make_speak(animal): print(animal.speak()) make_speak(Dog()) # "Woof!" make_speak(Cat()) # "Meow!" make_speak(Puppy()) # "Woof! Yip!"

Common Mistakes

✕ Forgetting super().__init__()
If the parent's __init__ sets up important attributes, skipping super() means those attributes won't exist on the child.
✕ Wrong method name
Overriding requires the exact same name. A typo like speek() creates a new method instead of overriding speak().
✕ Forgetting parentheses
Writing class Dog: instead of class Dog(Animal): means no inheritance at all: Dog is an independent class.

Summary Comparison

Inheritance Overriding super()
What it does Passes all methods to child Replaces a parent method Calls the parent version
Syntax class Child(Parent) Same method name in child super().method()
Purpose Code reuse Customization Extend, not replace
Required? Yes, to build hierarchy Optional per method Optional but recommended

Question 1 of 5

Which syntax correctly makes Cat inherit from Animal?

Question 2 of 5

What does Penguin().fly() return?

Question 3 of 5

What is the purpose of super().__init__() inside a child class's __init__ method?

Question 4 of 5

What does c.area() return and why?

Question 5 of 5

What is the output of the code above?