Inheritance
Inheritance lets a class inherit attributes and methods from another class. The child class extends the parent class, promoting code reuse.
20 min•By Priygop Team•Updated 2026
Inheritance
Inheritance
# Parent class
class Animal:
def __init__(self, name, sound):
self.name = name
self.sound = sound
def speak(self):
return f"{self.name} says {self.sound}!"
def info(self):
return f"{self.name} is an animal"
# Child classes
class Dog(Animal):
def __init__(self, name, breed):
super().__init__(name, "Woof")
self.breed = breed
def fetch(self):
return f"{self.name} fetches the ball!"
class Cat(Animal):
def __init__(self, name, indoor=True):
super().__init__(name, "Meow")
self.indoor = indoor
# Usage
dog = Dog("Rex", "Labrador")
cat = Cat("Whiskers")
print(dog.speak()) # Rex says Woof!
print(dog.fetch()) # Rex fetches the ball!
print(dog.info()) # Rex is an animal
print(cat.speak()) # Whiskers says Meow!
# Check inheritance
print(isinstance(dog, Dog)) # True
print(isinstance(dog, Animal)) # True
print(issubclass(Dog, Animal)) # True
# Multiple inheritance
class FlyingFish(Animal):
def fly(self):
return f"{self.name} can fly!"
def swim(self):
return f"{self.name} can swim!"
ff = FlyingFish("Nemo", "Splash")
print(ff.fly(), ff.swim())Tip
Tip
Use super().__init__() to call the parent constructor. This ensures parent attributes are properly initialized.
Diagram
Loading diagram…
Inheritance lets child classes reuse parent code
Common Mistake
Warning
Forgetting super().__init__() in child class means parent attributes aren't set. Always call super() first in child __init__.
Practice Task
Note
(1) Create Animal parent with Dog/Cat children. (2) Add unique methods to each child. (3) Use isinstance() and issubclass() to verify.