DEV Community

Cover image for Inheritance in Python: Types, Syntax, and Examples
Rachit Joshi
Rachit Joshi

Posted on

Inheritance in Python: Types, Syntax, and Examples

Inheritance is one of the important concepts of Object-Oriented Programming (OOP) in Python. It allows a class to acquire the properties and methods of another class, making it easier to reuse existing code and create relationships between classes.

In Python, a new class can inherit from one or more existing classes. The class that provides the properties and methods is called the parent class or base class, while the class that inherits them is called the child class or derived class. Python officially supports both single and multiple inheritance.

What Is Inheritance in Python?

Inheritance is a mechanism in which a child class derives attributes and methods from a parent class.

For example, suppose we have a Vehicle class containing common functionality. We can create a Car class that inherits from Vehicle instead of writing the same functionality again.

Basic Syntax
class Parent:
# Parent class members
pass

class Child(Parent):
# Child class members
pass

Here, Child inherits from Parent.

Simple Example of Inheritance
class Animal:
def sound(self):
print("Animals make sounds")

class Dog(Animal):
def bark(self):
print("Dog barks")

d = Dog()

d.sound()
d.bark()
Output
Animals make sounds
Dog barks

The Dog class does not define the sound() method, but it can still use it because it inherits the method from Animal.

Types of Inheritance in Python

Python supports several common forms of inheritance.

1. Single Inheritance

In single inheritance, one child class inherits from one parent class.

class Vehicle:
def start(self):
print("Vehicle started")

class Car(Vehicle):
def drive(self):
print("Car is driving")

c = Car()
c.start()
c.drive()

The Car class inherits from the Vehicle class.

2. Multiple Inheritance

In multiple inheritance, a child class inherits from more than one parent class.

class Father:
def skills(self):
print("Gardening")

class Mother:
def talent(self):
print("Cooking")

class Child(Father, Mother):
pass

c = Child()
c.skills()
c.talent()

Python allows a class to have multiple base classes. When multiple inheritance is used, Python follows a Method Resolution Order (MRO) to determine where attributes and methods should be searched.

3. Multilevel Inheritance

In multilevel inheritance, a class inherits from another derived class.

class Grandparent:
def show_grandparent(self):
print("Grandparent")

class Parent(Grandparent):
def show_parent(self):
print("Parent")

class Child(Parent):
def show_child(self):
print("Child")

c = Child()

c.show_grandparent()
c.show_parent()
c.show_child()

Here, Child indirectly inherits the functionality of Grandparent through Parent.

4. Hierarchical Inheritance

In hierarchical inheritance, multiple child classes inherit from the same parent class.

class Animal:
def eat(self):
print("Animal eats")

class Dog(Animal):
def bark(self):
print("Dog barks")

class Cat(Animal):
def meow(self):
print("Cat meows")

dog = Dog()
cat = Cat()

dog.eat()
cat.eat()

Both Dog and Cat inherit the eat() method from Animal.

5. Hybrid Inheritance

Hybrid inheritance is a combination of two or more inheritance patterns. It can involve structures such as multiple and multilevel inheritance.

Because multiple inheritance can create complex class relationships, Python uses MRO to determine the order in which base classes are searched.

Method Overriding in Python

A child class can provide its own implementation of a method that already exists in the parent class. This is known as method overriding.

class Animal:
def sound(self):
print("Animal makes a sound")

class Dog(Animal):
def sound(self):
print("Dog barks")

d = Dog()
d.sound()
Output
Dog barks

The Dog implementation overrides the implementation inherited from Animal.

Using super() with Inheritance

The super() function can be used to access functionality from a parent class without directly referring to the parent class name.

class Animal:
def sound(self):
print("Animal makes a sound")

class Dog(Animal):
def sound(self):
super().sound()
print("Dog barks")

d = Dog()
d.sound()
Output
Animal makes a sound
Dog barks

This approach is particularly useful when a child class wants to extend the behavior of an inherited method instead of completely replacing it.

Advantages of Inheritance

Inheritance provides several benefits in Python programming:

Code reusability: Existing methods and attributes can be reused.
Reduced duplication: Common functionality can be placed in a parent class.
Easy maintenance: Changes to shared functionality can often be made in one place.
Extensibility: Child classes can add new functionality to existing classes.
Supports polymorphism: Different derived classes can provide their own implementations of inherited methods.
isinstance() and issubclass()

Python provides built-in functions for working with inheritance.

isinstance() checks whether an object is an instance of a particular class or a class derived from it.

class Animal:
pass

class Dog(Animal):
pass

d = Dog()

print(isinstance(d, Dog))
print(isinstance(d, Animal))

Output:

True
True

issubclass() checks whether one class is derived from another class.

print(issubclass(Dog, Animal))

Output:

True

Python's documentation specifically identifies isinstance() and issubclass() as useful built-in functions for checking inheritance relationships.

Why Is Inheritance Important in Python?

Inheritance helps programmers model relationships between classes using an “is-a” relationship. For example, a Car is a Vehicle, and a Dog is an Animal.

It is especially useful when several classes share common behavior but also need their own specialized functionality.

However, inheritance should be used when the relationship between classes genuinely makes sense. For some designs, composition can be a better alternative.

Conclusion

Inheritance is a fundamental feature of Python's Object-Oriented Programming system. It allows classes to reuse and extend the functionality of existing classes while reducing unnecessary code duplication.

Python supports single, multiple, multilevel, hierarchical, and hybrid inheritance. Features such as method overriding, super(), isinstance(), issubclass(), and Method Resolution Order make inheritance flexible and powerful.

Top comments (0)