Multilevel, Hierarchical and Hybrid Inheritance
- Multi-Level Inheritance in Python
#Multilevel Inheritance in Python class India: def first(self): print ('India is the largest democratic country in the world') class Karnataka(India): def sec(self): print ('Karnataka is famous for IT and Coffee Production') class Bengaluru(Karnataka): def third(self): print ('Bengaluru also known as Garden city!') b=Bengaluru() b.first() b.sec() b.third()
Output of the above program is :
India is the largest democratic country in the world
Karnataka is famous for IT and Coffee Production
Bengaluru also known as Garden city!
2. Hierarchical Inheritance in Python example
# Hierarchial Inheritance using Python class Details: def __init__(self): self.__id="" self.__name="" self.__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="" self.__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="" self.__dept="" def setDoctor(self,id,name,gender,hos,dept): self.setData(id,name,gender) self.__hospital=hos self.__dept=dept def showDoctor(self): self.showData() print("Hospital: ", self.__hospital) print("Department: ", self.__dept) def main2(): print("Employee Object") e=Employee() e.setEmployee(1,"SHARAN","Male","ITC","Sales Manager") e.showEmployee() print("\nDoctor Object") d = Doctor() d.setDoctor(1, "Ramesh", "Male", "AIIMS", "ENT") d.showDoctor() main2()
Output of the above program is :
Employee Object
Id: 1
Name: SHARAN
Gender: Male
Company: ITC
Department: Sales ManagerDoctor Object
Id: 1
Name: Ramesh
Gender: Male
Hospital: AIIMS
Department: ENT
3. Hybrid Inheritance in Python
#Hybrid Inheritance using Python class Emp: cname="MIT India" class SystemAdmin(Emp): srole="System Admin" class WebDev(Emp): wrole="UI Developer" class DBA(WebDev, SystemAdmin,Emp): drole="Data Base Creating" obj=DBA() print("Company Name:"+obj.cname) print("SysAdmin:"+obj.srole) print("WebDev:"+obj.wrole) print("DBA:"+obj.drole)
Output of the above program is :
Company Name:MIT India
SysAdmin:System Admin
WebDev:UI Developer
DBA:Data Base Creating