Types of Inheritance in Python



Types Of Inheritance

1.Single Inheritance.
2.Multiple Inheritance.
3.Multilevel Inheritance.
4.Hierarchical Inheritance.
5.Hybrid Inheritance.

Single Inheritance

When inheritance involves a derived class that derives its properties from a single base class. It is called single Inheritance.



Syntax:

Class base:
    >Statement<
class Derived(base):
    >Statement<

For Example: 

class Employee:
    def Sallary(self):
        print("Sallary is : 54000")
class Person(Employee):
    def Name(self):
        print("Name is : John")
Per_Info=Person()
Per_Info.Sallary()
Per_Info.Name()



 

Multiple Inheritance


In the case of Multiple inheritance , a subclass derives its attributes from two or more classes.



Syntax:

Class base1:
    >statement<
Class base2:
    >statement<
Class derived(base1,base2):
    >statement<

For example: 

class Employee:
    def Sallary(self):
        print("Sallary is : 54000")
class Address:
    def address(self):
        print("Mumbai")
class Person(Employee,Address):
    def Name(self):
        print("Name is : John")
Per_Info=Person()
Per_Info.Sallary()
Per_Info.Name()
Per_Info.address()



Multilevel Inheritance

In the case of Multilevel inheritance, Features of both base class and derived class are further inherited to another class.



Syntax:

class base:
    <<statement>>
class derived(base):
    <<statement>>
class derived_base(derived):
    <<statement>>
    

For example:
 
class Employee:
    def Sallary(self):
        print("Sallary is : 54000")
class Address(Employee):
    def address(self):
        print("Mumbai")
class Person(Address):
    def Name(self):
        print("Name is : John")
Per_Info=Person()
Per_Info.Sallary()
Per_Info.Name()
Per_Info.address()

 


Hierarchical Inheritance

In the case of a hierarchical inheritance, when two or more class is derived from one base class is known as hierarchical inheritance.



Syntax:

class base:
    statement
class derived(base):
    statement
class derived_2(base):
    statement
class derived_3(base):
    statement

For example: 

class Employee:
    def Sallary(self):
        print("Sallary is : 54000")
class Address(Employee):
    def address(self):
        print("Mumbai")
class Person(Employee):
    def Name(self):
        print("Name is : John")
Per_Info=Person()
Add_Info=Address()
Per_Info.Sallary()
Add_Info.Sallary()

 

Hybrid Inheritance

Hybrid Inheritance consists of multiple types of inheritance.



Syntax:

class base:
    statement
class derived_1(base):
    statement
class derived_2(base):
    statement
class derived_3(derived_1,derived_2):
    statement

For example:

class Employee:
    def Sallary(self):
        print("Sallary is : 54000")
class Address(Employee):
    def address(self):
        print("Mumbai")
class Person(Employee):
    def Name(self):
        print("Name is : John")
class Contact(Address,Person):
    def show(self):
        print("Contact no is : 0124-457896")
Con_Info=Contact()
Con_Info.Name()
Con_Info.Sallary()
Con_Info.address()
Con_Info.show()

 
 

Post a Comment

0 Comments