What is Inheritance in Python?
Inheritance is one of the most important and powerful features of object-oriented programming. Inheritance refers to the property in which a class can inherit properties of another class that is the derived class can access all the properties, functions, and data members of the base class from which it has been inherited. To compare the feature with real-time, children get characteristics and behaviors inherited from their parents. Inheritance in Python is provided for the same purpose as explained.
Advantages of Inheritance:
- Reusability of code – since one can inherit from the base class, the efficiency of the code increases and the size of the code also decreases instead of rewriting the code.
- It forms a chain process i.e. if a child class is inherited from the base class, and if a new class is inherited from the child class, then all the properties of the base class will also be present in the new class.
Syntax:
Class base_class: Body of base_class Class child_class (base_class): Body of child_class
Example
class Parent: def __init__(self , fname, fage): self.firstname = fname self.age = fage def view(self): print(self.firstname , self.age) class Child(Parent): def __init__(self , fname , fage): Parent.__init__(self, fname, fage) self.lastname = "Machine Learning" def view(self): print(self.firstname ," came ", self.age , " years ago. Today, python is the base for" , self.lastname) object = Child("Python" , '28') object.view()
Output
Python came 28 years ago. Today, python is the base for Machine Learning
Types of inheritance:
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
Single inheritance
In single inheritance, only one child class can be inherited from the base class. Not more than one class can be used for inheritance.
Syntax:
Class base_class: Body of base_class Class child_class (base_class): Body of child_class
Example
class Base: def __init__(self , fname, fage): self.firstname = fname self.age = fage def view(self): print(self.firstname , self.age) class Child(Base): def __init__(self , fname , fage): Base.__init__(self, fname, fage) self.lastname = "Machine Learning" def view(self): print(self.firstname ," is ", self.age , " years old and he is a pro in " , self.lastname) object = Child("Raj" , '21') object.view()
Output
Raj is 21 years old and he is a pro in Machine Learning
Multiple inheritance
In multiple inheritance, a child class can be inherited from more than one base class. Unlike single inheritance, there is no restriction on the number of base classes for inheriting the properties.
Syntax:
Class Base1(): Body of base1 Class of Base2(): Body of base2 Class Child (Base1,Base2): Body of child
Example
class mom: mom_name = "" def mom(self): print(self.mom_name) class dad: dad_name = "" def dad(self): print(self.dad_name) class daughter(mom,dad): def parents(self): print("Father's name: ",self.dad_name) print("Mother's name: ",self.mom_name) object=daughter() object.dad_name="raj" object.mom_name="nandhini" object.parents()
Output
Father's name: raj Mother's name: nandhini
Multilevel inheritance
The multilevel inheritance is like a chain process. In this type, a class is derived from a base class and it becomes a base class for another class that is another new class is derived from this derived class.
Syntax:
Class Base: Body of base class Class Derived1 (Base): Body of derived1 class Class Derived2 (Derived1): Body of derived2 class
Example
class Granddad: def __init__(self, grandfather_name): self.grandfather_name = grandfather_name class dad(Granddad): def __init__(self, father_name, grandfather_name): self.father_name = father_name Granddad.__init__(self, grandfather_name) class Son(dad): def __init__(self,son_name, father_name, grandfather_name): self.son_name = son_name dad.__init__(self, father_name, grandfather_name) def print_name(self): print('Grandfather name :', self.grandfather_name) print("Father name :", self.father_name) print("Son name :", self.son_name) s1 = Son('Lucky', 'Raj', 'Kamal') s1.print_name()
Output
Grandfather name : Kamal Father name : Raj Son name : Lucky
Hierarchical inheritance
In hierarchical inheritance from a single base class, we can inherit multiple derived classes. There is no restriction on the number of derived classes.
Syntax:
Class Base: Body of base class Class Derived1 (Base) : Body of derived1 class Class Derived2 (Base) : Body of derived2 class
Example
class Details: def __init__(self): self.__id="<No Id>" self.__name="<No Name>" self.__gender="<No Gender>" def setData(self,id,name,gender): self.__id=id self.__name=name self.__gender=gender def showData(self): print("Id: ",self.__id) print("Name: ", self.__name) print("Gender: ", self.__gender) class Employee(Details): #Inheritance def __init__(self): self.__company="<No Company>" self.__dept="<No Dept>" def setEmployee(self,id,name,gender,comp,dept): self.setData(id,name,gender) self.__company=comp self.__dept=dept def showEmployee(self): self.showData() print("Company: ", self.__company) print("Department: ", self.__dept) class Doctor(Details): #Inheritance def __init__(self): self.__hospital="<No Hospital>" self.__dept="<No Dept>" def setEmployee(self,id,name,gender,hos,dept): self.setData(id,name,gender self.__hospital=hos self.__dept=dept def showEmployee(self): self.showData() print("Hospital: ", self.__hospital) print("Department: ", self.__dept) def main(): print("Employee Object") e=Employee() e.setEmployee(1,"Prem Sharma","Male","gmr","excavation") e.showEmployee() print("\nDoctor Object") d = Doctor() d.setEmployee(1, "pankaj", "male", "aiims", "eyes") d.showEmployee() if __name__=="__main__": main()
Output
Employee Object Id: 1 Name: Prem Sharma Gender: Male Company: gmr Department: excavation Doctor Object Id: 1 Name: pankaj Gender: male Hospital: aiims Department: eyes
Hybrid inheritance
The hybrid inheritance is a combination of other types of inheritance. Mostly it is a combination of multilevel and multiple inheritance. This type of inheritance is not much used.
In general, multiple and multilevel inheritance is widely used in real-time problems. Single inheritance is used for simple applications.
Check this video for more:
Video credits– Edureka
Also Read:
10 Tkinter Projects for Beginners
Introduction Python has become one of the fastest-growing programming languages, and its applications are also increasing every day. Tkinter Projects for Beginners are one of the best ways to learn the basics of Python. Tkinter is said to be one of the most straightforward modules to start learning Python as well. This will help absolute…
10 Useful Python One-Liners
In this tutorial, we will learn about the best and most powerful Python One-Liners ranging from beginner’s level to advanced level that too with examples for better understanding. Using Python One-Liners in your programs will actually make your code more readable and precise. What exactly are Python One-Liners? One-liners in simple language are single-line codes…
100+ Java Projects for Beginners 2023
Are you looking for interesting Java Projects ideas and 100+ Java Projects for beginners with source code? If yes, the you have come to right place because in this article we will show you best 100+ Java project ideas and topics for beginners with source code. We recommend you bookmark this page before you lost it as…
15 Common Coding Mistakes by Beginners
The most common mistakes that beginners while learning to code are 1. Switching between multiple languages 2. No roadmap 3. Lone wolf programming
15 Deep Learning Projects for Final year
Introduction In this tutorial, we are going to learn about Deep Learning Projects for Final year students. It contains all the beginner, intermediate and advanced level project ideas as well as an understanding of what is deep learning and the applications of deep learning. What is Deep Learning? Deep learning is basically the subset of…
20 Python Projects for Resume
About 20 Python Projects for Resume Today, in this article on 20 Python Projects for Resume, we’ll expose you to some of the top beginners to advanced python projects for resumes. In 2022, your best investment will be learning Python through practical projects. Therefore, working on actual Python projects is essential if you wish to…