Course Progress57%
🍎 Python OOP Topic 57 / 100
⏳ 8 min read

Inheritance

Build new classes from existing ones — inherit all their attributes and methods, then add or customise what you need.

"Inheritance is reuse done right. Instead of copying code into a new class, you say 'this new class is a kind of that class' and get everything for free."

— ShurAI

What is Inheritance?

Inheritance lets you create a new class that automatically gets all the attributes and methods of an existing class, plus its own additions. The existing class is the parent; the new class is the child:

Inheritance tree
Animal
↓ inherits
Dog
Cat
Bird
Dog, Cat, and Bird all get Animal’s methods for free. Each can also add its own.

Basic Inheritance

python
class Animal:                     # parent class
    def __init__(self, name):
        self.name = name

    def breathe(self):
        print(f"{self.name} breathes air.")

    def sleep(self):
        print(f"{self.name} is sleeping.")


class Dog(Animal):                 # Dog inherits from Animal
    def bark(self):                 # Dog's own method
        print(f"{self.name} says: Woof!")


d = Dog("Bruno")
d.breathe()   # Bruno breathes air. — inherited from Animal
d.sleep()     # Bruno is sleeping.  — inherited from Animal
d.bark()      # Bruno says: Woof!   — Dog's own method

Child Class with Its Own __init__

The child can define its own __init__ and call the parent’s using super():

python
class Animal:
    def __init__(self, name):
        self.name = name

    def describe(self):
        print(f"Name: {self.name}")


class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)    # call Animal's __init__ first
        self.breed = breed        # then add Dog's own data

    def describe(self):
        super().describe()        # call parent's describe()
        print(f"Breed: {self.breed}")

d = Dog("Bruno", "Labrador")
d.describe()
# Name: Bruno
# Breed: Labrador

Checking Inheritance with isinstance()

python
d = Dog("Bruno", "Labrador")

print(isinstance(d, Dog))     # True  — d is a Dog
print(isinstance(d, Animal))  # True  — d is also an Animal
print(isinstance(d, Cat))     # False — d is not a Cat

Real Example — Employee Hierarchy

python
class Employee:
    def __init__(self, name, salary):
        self.name   = name
        self.salary = salary

    def details(self):
        print(f"{self.name} — ₹{self.salary}")


class Manager(Employee):
    def __init__(self, name, salary, team_size):
        super().__init__(name, salary)
        self.team_size = team_size

    def details(self):
        super().details()
        print(f"  Manages a team of {self.team_size}")

e = Employee("Riya",  60000)
m = Manager("Arjun", 90000, 8)

e.details()
m.details()
output
Riya — ₹60000
Arjun — ₹90000
  Manages a team of 8

"Use inheritance when there is a clear 'is a' relationship: a Dog IS an Animal. A Manager IS an Employee. If you can’t say 'is a', composition is probably better."

— ShurAI

🧠 Quiz — Q1

What syntax creates a Dog class that inherits from Animal?

🧠 Quiz — Q2

If Dog inherits from Animal, which methods does a Dog object have?

🧠 Quiz — Q3

isinstance(d, Animal) returns True even though d is a Dog. Why?

🧠 Quiz — Q4

When a child class defines its own __init__, what must you do to set up the parent's attributes too?