Last Updated on March 13, 2023 by Prepbytes
Python is one of the programming languages that support object-oriented programming. And in Object oriented programming inheritance is an important aspect as it is one of the four pillars of OOPs. With the help of inheritance, we can inherit the feature of one class in another class and use them without writing them again. There are various types of inheritance in python we will discuss all of them while moving further in this article with their examples.
What is Inheritance in Python?
Like in any object-oriented programming language inheritance in python can be understood as a mechanism by which we can derive a new class from an existing class we can inherit all the methods, and attributes of the class. The class from which we are deriving is known as the parent class and the class in which we are deriving the attributes and methods is known as the subclass or child class.
The main purpose of inheritance is to allow the user to code reuse and creator a more specialized class from a general class. There is no boundation on the child class as it can add new functions or attributes and even it can override the existing ones. By overriding the existing attributes the access to the parent class’s methods and attributes still remains.
To create a subclass in python, the child class is defined with the name of the parent class in parentheses this allows the child class to inherit all of the attributes and methods of the parent class. From there the child class can modify or add new attributes and methods as needed. We can simply say that inheritance is a great way to reuse the code.
Types of Inheritance in Python
There are mainly 5 types of inheritance in python.
The 5 types of inheritance in python are named below:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance.
We will discuss each type of inheritance in python in detail with their examples and syntax below.
Single Inheritance
Single inheritance can be referred to as the most basic inheritance of all types of inheritance in python. As the name suggests in this we derive a single base class from the single parent class or the existing class. The child class will take or inherit all the attributes of the parent class. It can also add its own methods and attributes or override the methods of the parent class.
Syntax of Single Inheritance in Python
The syntax of single inheritance in python is given below:
class ParentClass:
# Parent class definition
class ChildClass(ParentClass):
# Child class definition
In the above syntax we derive the child class from the parent class.
Example of Single Inheritance in Python
Below is an example of single inheritance with code and explanation.
# Parent Class class Animal: def __init__(self, name): self.name = name def sound(self): pass # Child Class class Dog(Animal): def sound(self): return "Bark!" dog = Dog("Buddy") print(dog.name) print(dog.sound())
Output
Buddy
Bark!
Explanation of the above example
Here, the Animal class is the parent class, and the Dog class is the child class. The Dog class inherits the name attribute from the Animal class through the init method. The sound method is overridden in the Dog class to return a specific sound for dogs.
Multiple Inheritance
Multiple inheritance is a type of inheritance in which a new class is derived from two or more existing classes.
We can say that there is a single-child class and there are two or more parent classes. The new class inherits all the attributes and methods of the parent classes. The child class can access all the attributes and methods of both parent classes.
Syntax of Multiple Inheritance in Python
The syntax for multiple inheritances is given below:
class ParentClass1:
# Parent class 1 definition
class ParentClass2:
# Parent class 2 definition
class ChildClass(ParentClass1, ParentClass2):
# Child class definition
In the above syntax we have two parent classes and one child class.
Example of Multiple Inheritance in Python
We will now see the example of the multiple inheritances with code and proper implementation followed by its explanation.
# Parent Class 1 class Person: def __init__(self, name): self.name = name def show_name(self): return self.name # Parent Class 2 class Employee: def __init__(self, salary): self.salary = salary def show_salary(self): return self.salary # Child Class class Manager(Person, Employee): def __init__(self, name, salary): Person.__init__(self, name) Employee.__init__(self, salary) manager = Manager("John Doe", 5000) print(manager.show_name()) print(manager.show_salary())
Output
John Doe
5000
Explanation of the above example
Here, the Person and Employee classes are parent classes, and the Manager class is the child class. The Manager class inherits the show_name method from the Person class and the show_salary method from the Employee class. The init method of the Manager class is called to initialize the attributes inherited from both parent classes.
Multi-level Inheritance
Multi-level inheritance is a type of inheritance in which a new class is derived from a parent class, which in turn is derived from another parent class.
As the name suggests this is not like other types of inheritance in python we have studied till now here we are deriving from the already derived class. The new class inherits all the attributes and methods of both parent classes. The child class can access all the attributes and methods of both parent classes.
Syntax of Multi-level Inheritance in Python
The syntax for multilevel inheritance is given below:
class GrandParentClass:
# Grandparent class definition
class ParentClass(GrandParentClass):
# Parent class definition
class ChildClass(ParentClass):
# Child class definition
In the above syntax we have named the main class as GrandParentClass and the derived class from it as ParentClass and the new derived class as ChildClass.
Example of Multi-level Inheritance in Python
Below is an example of multiple inheritance in python with code and its implementation.
# Grandparent Class class Animal: def __init__(self, name): self.name = name def show_name(self): return self.name # Parent Class class Dog(Animal): def sound(self): return "Bark!" # Child Class class Bulldog(Dog): def run(self): return "Running!" bulldog = Bulldog("Buddy") print(bulldog.show_name()) print(bulldog.sound()) print(bulldog.run())
Output
Buddy
Bark!
Running!
Explanation of the above example
Here, the Animal class is the grandparent class, the Dog class is the parent class, and the Bulldog class is the child class. The Bulldog class inherits the show_name method from the Animal class and the sound method from the Dog class. Additionally, the Bulldog class defines its own method run.
Hierarchical Inheritance
This is different from all the previous types of inheritance in python that we have studied as in this we can derive multiple child classes does not even needing many parent classes. So Hierarchical inheritance can be referred to as type of inheritance in which a new class is derived from a single-parent class, and multiple child classes are derived from the same parent class. All child classes inherit all the attributes and methods of the parent class. The child classes can also add their attributes and methods or override the methods of the parent class.
Syntax of Hierarchical Inheritance in Python
The syntax for hierarchical inheritance is given below:
class ParentClass:
# Parent class definition
class ChildClass1(ParentClass):
# Child class 1 definition
class ChildClass2(ParentClass):
# Child class 2 definition
There are two child classes derived from a single parent class in the above syntax.
Example of Hierarchical Inheritance in Python
Below is an example of hierarchical inheritance in python.
# Parent Class class Animal: def __init__(self, name): self.name = name def sound(self): pass # Child Class 1 class Dog(Animal): def sound(self): return "Bark!" # Child Class 2 class Cat(Animal): def sound(self): return "Meow!" dog = Dog("Buddy") cat = Cat("Mittens") print(dog.name) print(dog.sound()) print(cat.name) print(cat.sound())
Output
Buddy
Bark!
Mittens
Meow!
Explanation of the above example
Here, the Animal class is the parent class, and the Dog and Cat classes are child classes. Both child classes inherit the name attribute from the parent class through the init method. Each child class overrides the sound method to return a specific sound for each animal type.
Hybrid Inheritance
This is the last type of inheritance in python as the name suggests, Hybrid inheritance is a type of inheritance that combines two or more types of inheritance, such as single inheritance and multiple inheritances. The syntax for defining a child class with hybrid inheritance is similar to that of multiple inheritances.
Syntax of Hybrid Inheritance in Python
The syntax of hybrid inheritance is given below.
class ParentClass1:
# Parent class 1 definition
class ParentClass2:
# Parent class 2 definition
class ChildClass(ParentClass1, ParentClass2):
# Child class definition
Example of Hybrid Inheritance in Python
We will now see the example of hybrid inheritance with its code and explanation.
class A: def method_a(self): print("This is method A") class B(A): def method_b(self): print("This is method B") class C(A): def method_c(self): print("This is method C") class D(B, C): def method_d(self): print("This is method D") obj = D() obj.method_a() obj.method_b() obj.method_c() obj.method_d()
Output
This is method A
This is method B
This is method C
This is method D
Explanation of the above example
Class A is the base class, which has only one method method_a. Class B and C inherit from class A and have their own methods method_b and method_c respectively. Class D is a hybrid class that inherits from both B and C. It has its own method method_d.
When we create an object of class D, it has access to all the methods of its parent classes B and C, as well as its base class A. In hybrid inheritance, the order of inheritance is very important. In our example, class B is inherited first followed by class C. If we change the order, the behavior may change.
Applications of Inheritance in Python
Inheritance has many applications some of which are mentioned below:
- Code reuse: This is one of the major advantages of inheritance. With the help of this, we can use the existing code again in our derived classes.
- Modularity: This will make your code easy to maintain as we will follow a hierarchical structure.
- Customization: We can customize the class’s attributes by our choice there is no restriction on the types of attributes or methods that we have to use.
- Frameworks and libraries: Inheritance is used by many libraries and frameworks in python.
- Testing: Developers use inheritance to create test classes and inherit the details in the same manner as the derived classes.
Conclusion
Inheritance is referred to as one of the most essential concepts in object-oriented programming. We have studied various types of inheritance in python like single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance, and hybrid inheritance. Each type of inheritance has its own advantages and disadvantages the users must decide according to their use of the program.
With the help of inheritance, developers can create many complex programs with reusable code. The inheritance will also help to reduce errors and improve code maintainability.
Frequently Asked Questions
1. Mention some of the advantages of inheritance in Python.
Inheritance allows code reuse, promotes modularity, and helps reduce errors and redundant code.
2. How do you define a class in Python?
You can define a class in Python using the class keyword followed by the class name and a colon.
3. How do you inherit a class in Python?
You can inherit a class in Python by including the name of the parent class in parentheses after the class name of the child class.
4. What is the difference between inheritance and composition in Python?
Inheritance allows a class to inherit properties and methods from another class, while composition involves creating objects that contain other objects as components.
5. When should you use inheritance in Python?
You should use inheritance in Python when you need to reuse code and promote modularity, and when you need to create a hierarchy of related classes. However, you should use inheritance judiciously and avoid creating deeply nested class hierarchies, as this can lead to complexity and code coupling.