Inheritance is one of the features of OOP language. Through this feature, an OOP language can allow code reusability. A class can be inherited into another class or derived from another class. The class that is being inherited is known as base or parent class and the derived class is known as child class.
Instantiation can be done for both parent and child classes but it is conventional to declare objects from child class only since it is the most specialised version.
#Example code showing how a base class can be inherited
class person:
def getperson(self):
self.name=input(“Enter your name:”)
self.age=input(“Enter your age:”)
self.qual=input(“Enter your qualification :”)
class employee(person):
def getemp(self):
self.desg=input(“Enter your designation:”)
def showdata(self):
print(“Employee Details”)
print(“Name :”,self.name)
print(“age :”,self.age)
print(“qual :”,self.qual)
print(“desg :”,self.desg)
ramu=employee()
ramu.getperson()
ramu.getemp()
ramu.showdata()
Types of Inheritances
The following four types of inheritances are possible in python. They are:
1. Single
2. Multi level
3. Multiple
4. Hierarchical
Single or Single Level Inheritance
If one class is extended into another sub class, it is known as single inheritance. In other words, if the class from which object is declared is derived from one base class, it is known as single level inheritance
#Example
class person:
def getperson(self):
self.name=input(“Enter your name:”)
self.age=input(“Enter your age:”)
self.qual=input(“Enter your qualification :”)
class husband(person):
def gethus(self):
self.getperson()
self.wife=input(“Enter your spouse name:”)
def showdata(self):
print(“person Details”)
print(“Name :”,self.name)
print(“age :”,self.age)
print(“qual :”,self.qual)
print(“spouse :”,self.wife)
ramu=husband()
ramu.gethus()
ramu.showdata()
Multi Level Inheritance
If the class from which object is created is derived from a base class which is already derived from another base class, it is known as multi-level inheritance.
#Example
class person:
def getperson(self):
self.name=input(“Enter your name:”)
self.age=input(“Enter your age:”)
self.qual=input(“Enter your qualification :”)
class husband(person):
def gethus(self):
self.getperson()
self.wife=input(“Enter your spouse name:”)
class father(husband):
def getchild(self):
self.gethus()
self.child=input(“Enter your child name:”)
def showdata(self):
print(“person Details”)
print(“Name :”,self.name)
print(“age :”,self.age)
print(“qual :”,self.qual)
print(“spouse :”,self.wife)
print(“Child :”,self.child)
ramu=father()
ramu.getchild()
ramu.showdata()
Hierarchical Inheritance
If one base class is inherited into two or more child classes it is known as hierarchical Inheritance.
#Example
class person:
def getperson(self):
self.name=input(“Enter your name:”)
self.age=input(“Enter your age:”)
self.qual=input(“Enter your qualification :”)
class husband(person):
def gethus(self):
print(“Enter personal Details”)
self.getperson()
self.wife=input(“Enter your spouse name:”)
def showdata(self):
print(“person Details”)
print(“Name :”,self.name)
print(“age :”,self.age)
print(“qual :”,self.qual)
print(“spouse :”,self.wife)
class employee(person):
def getdesg(self):
print(“Enter Employee Details”)
self.getperson()
self.desg=input(“Enter your desgination:”)
def showdata(self):
print(“Employee Details”)
print(“Name :”,self.name)
print(“age :”,self.age)
print(“qual :”,self.qual)
print(“desg :”,self.desg)
ram=husband()
lakshman=employee()
ram.gethus()
lakshman.getdesg()
ram.showdata()
lakshman.showdata()
Multiple Inheritance
If a class is derived from more than one base class, it is known as multiple inheritance.
#Example
class person:
def getperson(self):
self.name=input(“Enter your name:”)
self.age=input(“Enter your age:”)
self.qual=input(“Enter your qualification :”)
class student:
def getstd(self):
print(“Enter student Details”)
self.course=input(“Enter your course name:”)
class employee(person,student):
def getdesg(self):
print(“Enter Employee Details”)
self.getperson()
self.getstd()
self.desg=input(“Enter your designation:”)
def showdata(self):
print(“Employee Details”)
print(“Name :”,self.name)
print(“age :”,self.age)
print(“qual :”,self.qual)
print(“course :”,self.course)
print(“desg :”,self.desg)
ram=employee()
ram.getdesg()
ram.showdata()