Back to: Python Programming
Inheritance allows us to define a class that inherits all the methods and properties from another class. Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a class (called a child or derived class) to inherit attributes and methods from another class (called a parent or base class). This promotes code reuse, modularity, and a hierarchical class structure.
Parent class is the class being inherited from, also called base class.
Child class is the class that inherits from another class, also called derived class.
We are going to create an Animal class, the Dog, Cat and Mouse class will inherit attributes and methods from the Animal class.

The Animals class will have attributes and methods that are common to all animals, so many can be passed down and inherited by child classes for other animals.
class Animal:
def __init__(self, name):
self.name = name
self.is_alive = True
def eat(self): # all animals eat - so this can be passed down
print(f"{self.name} is eating")
def sleep(self): # all animals sleep - so this can be passed down
print(f"{self.name} is asleep")
We can now define some child classes that can inherit the attributes and methods from our Parent class Animal.
For a child class to inherit the attributes and methods from another class, after the class name (that’s going to do the inheriting – the child) we need to add an inheritance list with a set of parenthesis, then list the name of the class we’re inheriting from (the parent).
class Dog(Animal):
pass # <-- use pass as a placeholder (it won't cause an error
Similarly we can add Cat and Mouse classes using the parent Animal class:
class Dog(Animal):
pass
class Cat(Animal):
pass
class Mouse(Animal):
pass
Finally we can create some objects. We’ll create a Dog object called Scooby.
dog = Dog("Scooby")
cat = Cat("Garfield")
mouse = Mouse("Mickey")
Even though there is nothing within these Dog, Cat or Mouse classes, we will still have the attributes and methods defined in the Animal class which has been inherited by them.
print(dog.name)
print(dog.is_alive)
dog.eat()
dog.sleep()
Outputs:
Scooby
True
Scooby is eating
Scooby is asleep
Even though the three children classes are empty we’re still inheriting the attributes and methods from its parent of Animal. This is convenient because you don’t need to copy and paste these attributes and methods for every single class for example. If I were to copy these attributes and methods and paste them, we’d have a lot more code to write and as a consequence if I need to make a change to one of these methods, I would have to do that to every single instance of this method.
For example:
Let’s replace “is asleep” with “is sleeping”. I’d need to find every single Sleep Method and change it manually. It’s not too bad if you only have a few classes but imagine if you have hundreds of classes that’s going to be a lot of work and take a lot of time.
It’s a lot easier to write the code once and then reuse it, we only need to make that change in one place rather than make the change many times so let’s change “is asleep” to “is sleeping” and see if this works!
Children classes can have their own individual attributes and methods in addition to those they inherit. Our animals can all speak, so we need to create method that will print a unique message for dogs, a unique message for cats and a unique message for mice.
class Dog(Animal):
def speak(self):
print("WOOF!")
The full program looks like this:
class Animal:
def __init__(self, name):
self.name = name
self.is_alive = True
def eat(self):
print(f"{self.name} is eating")
def sleep(self):
print(f"{self.name} is sleeping")
class Dog(Animal):
def speak(self):
print("WOOF!")
class Cat(Animal):
def speak(self):
print("MEOW!")
class Mouse(Animal):
def speak(self):
print("SQUEEK!")
dog = Dog("Scooby")
cat = Cat("Garfield")
mouse = Mouse("Mickey")
print(cat.name)
print(cat.is_alive)
cat.eat()
cat.sleep()
cat.speak()
Outputs:
Garfield
True
Garfield is eating
Garfield is sleeping
MEOW!
OOP Class Inheritance and Private Class Members – Python for Beginners! – (16m:11s)