The struggle for a clean code is a battle joined by all the programmers. And that battle can be conquered with a proper armour of object-oriented programming concepts. And proper utilization of OOP concepts helps us to improve code reusability, readability, optimal time and space complexity.
Inheritance is one of the most utilized object-oriented features and implementing it in python is an enthusiastic task. So, let’s start now!
First things first let’s understand the definition of inheritance.
Inheritance is a process of obtaining properties and characteristics(variables and methods) of another class. In this hierarchical order, the class which inherits another class is called subclass or child class, and the other class is the parent class.
Inheritance is categorized based on the hierarchy followed and the number of parent classes and subclasses involved.
There are five types of inheritances:
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Must read: Free excel courses!
This type of inheritance enables a subclass or derived class to inherit properties and characteristics of the parent class, this avoids duplication of code and improves code reusability.
i = 5
print(“Hey there, you are in the parent class”)
print(“Hey there, you are in the sub class”)
Alright, let’s walk through the above code.
In the above code “Above” is the parent class and “Below” is the child class that inherits the parent class. Implementing inheritance in python is a simple task, we just need to mention the parent class name in the parentheses of the child class. We are creating objects of both parent class and child class, and here comes an interesting point about the inheritance. A child class can access the methods and variables of the parent class, whereas the vice versa is not true.
So in the above code temp1 object can access both fun1 and fun2 methods whereas the temp2 object can access only the fun1 method. Similarly, the same rule applies to variables in the code. And accessing a child class method or variable from a parent class object will throw an error. If the last line in the code is uncommented then it raises an error.
Our learners also read – python free courses!
This inheritance enables a child class to inherit from more than one parent class. This type of inheritance is not supported by java classes, but python does support this kind of inheritance. It has a massive advantage if we have a requirement of gathering multiple characteristics from different classes.
|#parent class 1
#parent class 2
class C(A, B):
print(“Hey there, you are in the child class”)
# Main code
c = C()
c.demo1 = 10
c.demo2 = 5
print(“first number is : “,c.demo1)
print(“second number is : “,c.demo2)
In the above code, we’ve created two parent classes “A”, “B”. Following the syntax of the inheritance in python, we’ve created a child class, which inherits both classes “A” and “B”. As discussed earlier that a child class can access the methods and variables of the parent class, The child class “C” can access the methods of its parent class.
In multilevel inheritance, the transfer of the properties of characteristics is done to more than one class hierarchically. To get a better visualization we can consider it as an ancestor to grandchildren relation or a root to leaf in a tree with more than one level.
|#parent class 1
print(“vehicles are used for transportation”)
#child class 1
print(“a car has 4 wheels”)
#child class 2
print(“electric car runs on electricity”)
Having a dry run over the above code, we’ve created a class “vehicle”, then we’ve created a class car that inherits the class vehicle. Now the “vehicle” is a parent class and the “car” is a child class. Later we’ve created an “electric_car” class, now the car class is a parent class and the electric_car class is a child class, and the relationship between vehicle class and electric_car class is the multilevel inheritance.
Must read: Data structures and algorithm free!
Here electric_car class can access the methods, variables of both vehicle and car class, whereas car class can access only the methods, variables of vehicle class. And as discussed parent class vehicle cannot access any method of the child class.
This inheritance allows a class to host as a parent class for more than one child class or subclass. This provides a benefit of sharing the functioning of methods with multiple child classes, hence avoiding code duplication.
print(“Hey there, you are in the parent class”)
#child class 1
print(“Hey there, you are in the child class 1”)
#child class 2
print(“Hey there, you are in the child class 2”)
#child class 3
print(“Hey there, you are in the child class 3”)
# main program
child_obj1 = child3()
child_obj2 = child2()
child_obj3 = child1()
In the above code, we have a single parent class and multiple child classes inheriting the same parent class. Now all the child classes can access the methods and variables of the parent class. We’ve created a “Parent” class and 3 child classes “child1”, “child2”, “child3”, which inherits the same parent class “Parent”.
Checkout: Python Open Source Project Ideas
An inheritance is said hybrid inheritance if more than one type of inheritance is implemented in the same code. This feature enables the user to utilize the feature of inheritance at its best. This satisfies the requirement of implementing a code that needs multiple inheritances in implementation.
print(“Hey there, you are in class A”)class B(A):
print(“Hey there, you are in class B”)class C(A):
print(“Hey there, you are in class C”)class D(C,A): #line 13
print(“Hey there, you are in the class D”)#main program
ref = D()
In the above code, we can see that we’ve implemented more than one type of inheritance. Classes A, B, C implements hierarchical inheritance, and classes A, C, D implements multilevel inheritance. Noe those individual inheritances have their individual properties of accessing methods and variables of the parent class. Also, there’s a point to be noted.
When we are implementing multilevel inheritance we follow syntax like “child_class(parent_class1, parent_class2)”. But this syntax will throw an error if “parent_class1” is hierarchically above the “parent_class2”. If we want to implement this syntax, then the “parent_class1” must be in a hierarchically lower level than “parent_class2”. For example in the above code, if line 13 has a syntax class D(A, C) then the code wouldn’t work since class C is hierarchically lower than class A.
Learn data science courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.
We’ve gone through the uses and needs of inheritance and understood the definition of inheritance. Also, we’ve gone through the types of inheritance and walked through the implementation codes and explanations of each type of inheritance. Understood the rules of variables and method accessing in different types of inheritances.
Now that you are aware of different types of inheritances in python, try implementing them and try utilizing them in your code. Try optimizing your code with the proper utilization of inheritance.
If you are curious to learn about data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.
What is the difference between multiple inheritance and multilevel inheritance?
Many beginner programmers often get confused between multiple inheritance and multilevel inheritance. The following illustrates some of the significant differences between these two types of inheritance.
Multiple Inheritance -
1. When a child class inherits its properties and characteristics from more than one base class, such type of inheritance is known as Multiple Inheritance.
2. It is not used widely since the Multiple Inheritance can be quite complex to understand.
3. It has only two class levels: base class and derived class.
1. The inheritance in which a child class inherits the properties from its base class which is further inheriting the properties from another base class, making the former a child class is known as Multilevel Inheritance.
2. This inheritance serves great purposes and hence is used much widely.
3. It has at least three class levels: base class, intermediate class, and derived class.
What do you understand about Hybrid Inheritance?
Hybrid Inheritance is a unique type of inheritance. Rather than having a new concept, as its name suggests it is a combination of two or more types of inheritances. For example, a class showing both multiple and multilevel inheritances is an example of hybrid inheritance.
What do you know about access modifiers in Python?
In Python, there are 3 types of access modifiers that are mentioned below:
1. Public Access Modifier - The Public members of a class are accessible by any part of the program. In Python, if the access specifier of data members or member functions is not specified then it is public by default.
2. Protected Access Modifier - If a data member or a member function is defined as protected then it is only accessible by the derived classes of that class.
3. Private Access Modifier - The private access modifier is the most secure access specifier. The private members are only accessible within the class in which they are defined.