Python Inheritance

In Python, inheritance is a fundamental concept of Object-Oriented Programming (OOP) that allows one class (called the child or derived class) to inherit attributes and methods from another class (called the parent or base class). This promotes code reusability, efficiency, and organization by allowing new classes to build upon existing classes, reducing redundancy.

What is Inheritance?

Inheritance enables you to create a new class that is a modified or extended version of an existing class. The new class (child class) inherits the properties and behaviors (methods) of the existing class (parent class), and it can also have additional properties and methods of its own.

How to Implement Inheritance in Python

In Python, inheritance is implemented by defining a new class that includes the name of the parent class in parentheses after the name of the child class.

Types of Inheritance

Python supports various types of inheritance, which determine how a class can derive properties and methods from one or more parent classes.

1. Single Inheritance

In single inheritance, a child class inherits from one parent class. This is the simplest form of inheritance.

Example


class Parent:
def greet(self):
    return "Hello from Parent!"

class Child(Parent):
def play(self):
    return "Child is playing."

obj = Child()
print(obj.greet())  # Inherited from Parent
print(obj.play())   # Defined in Child
            

Output

Hello from Parent! Child is playing.

Explanation of Code Example

In this example:
  • We created a parent class called Animal with an attribute species and a method make_sound().
  • The child class Dog inherits from the Animal class and overrides the make_sound() method.
  • We created an object of the Dog class and called the make_sound() method, which outputs "Bark!".

2. Multiple Inheritance

In multiple inheritance, a child class inherits from more than one parent class. This allows combining features of multiple classes.

Example


class Parent1:
def greet(self):
    return "Hello from Parent1!"

class Parent2:
def welcome(self):
    return "Welcome from Parent2!"

class Child(Parent1, Parent2):
def play(self):
    return "Child is playing."

# Creating an object of the Child class
obj = Child()
print(obj.greet())    
print(obj.welcome())  
print(obj.play())
            

Output

Hello from Parent1! Welcome from Parent2! Child is playing.

Use Case:

Multiple inheritance is useful when a class needs to inherit different functionalities from multiple classes. For example, a FlyingCar class can inherit from both Car and Aircraft classes.

Method Resolution Order

The Method Resolution Order (MRO) determines the sequence in which classes are searched for a method or attribute when it is called. Use the mro() method or __mro__ attribute to check the order.

Example


class A:
def show(self):
    return "A"

class B(A):
pass

class C(A):
def show(self):
    return "C"

class D(B, C):
pass

print(D.mro())  # Displays the method resolution order
obj = D()
print(obj.show())  # Output: C
            

Output

[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 'object'>]
C

3. Multilevel Inheritance

In multilevel inheritance, a class inherits from a parent class, and another class inherits from that derived class. It forms a chain of inheritance.

Example


class Grandparent:
def family_name(self):
    return "Smith"

class Parent(Grandparent):
def greet(self):
    return "Hello from Parent!"

class Child(Parent):
def play(self):
    return "Child is playing."

# Creating an object of the Child class
obj = Child()
print(obj.family_name())  
print(obj.greet())        
print(obj.play())  
            

Output

Smith Hello from Parent! Child is playing.

Use Case:

Multilevel inheritance is used to represent a natural hierarchy. For example:

Key Differences Between Multiple and Multilevel Inheritance

Feature Multiple Inheritance Multilevel Inheritance
Definition A class inherits from more than one parent class. A class inherits from a class, which itself inherits from another class.
Structure Combines functionalities from multiple unrelated classes. Creates a hierarchy of classes, with inheritance forming a chain.
Use Case Useful for mixing functionalities. Example: FlyingCar. Useful for modeling hierarchies. Example: Family Tree.
Complexity More complex due to potential method conflicts. Simpler as it involves a linear chain.
Ambiguity Handling Resolved using Method Resolution Order (MRO). Less ambiguity due to linear structure.

4. Hierarchical Inheritance

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

Example


class Parent:
def greet(self):
    return "Hello from Parent!"

class Child1(Parent):
def play(self):
    return "Child1 is playing."

class Child2(Parent):
def study(self):
    return "Child2 is studying."

# Creating objects of the child classes
obj1 = Child1()
obj2 = Child2()
print(obj1.greet())  # Output: Hello from Parent!
print(obj1.play())   # Output: Child1 is playing.
print(obj2.greet())  # Output: Hello from Parent!
print(obj2.study())  # Output: Child2 is studying.  

Output

Hello from Parent! Child1 is playing. Hello from Parent! Child2 is studying.

5. Hybrid Inheritance

Hybrid inheritance is a combination of two or more types of inheritance.

Example


class Base:
def base_method(self):
    return "Base method."

class Parent1(Base):
def parent1_method(self):
    return "Parent1 method."

class Parent2(Base):
def parent2_method(self):
    return "Parent2 method."

class Child(Parent1, Parent2):
def child_method(self):
    return "Child method."

# Creating an object of the Child class
obj = Child()
print(obj.base_method())     # Output: Base method.
print(obj.parent1_method())  # Output: Parent1 method.
print(obj.parent2_method())  # Output: Parent2 method.
print(obj.child_method())    # Output: Child method.

Output

Base method. Parent1 method. Parent2 method. Child method.

Advantages of Inheritance

Inheritance is a powerful feature in Python that helps you write clean, modular, and reusable code. By understanding how inheritance works, you'll be able to take advantage of the full potential of Object-Oriented Programming to build complex and efficient applications.