× Python Introduction What is Python Python Features Python History Python Applications Python Install Python Path Python Example Execute Python Keywords Constant Variable Statements & Comments Python I/O and Import Operators UnaryBinaryTernary Unary Operators Unary Minus Binary Operators Arithmetic Operators Assignment Operators Relational Operators Logicaloperators Bitwise Operator Ternary Operators Control Statements in Python conditonal Statements IF if else Else If Nested if Switch For loop Nested For Loop While Loop Nested while Loop Unconditonal Statemets Continue Break Pass FUNCTIONS Python Function Function Argument Python Recursion Anonymous Function Python Modules NATIVE DATATYPES Python List Python Numbers Python Tuple Python String Python Set Python Dictionary OOPS PRINCIPALS Encapsulation Class Variable Method Object Or Instance CreationMethod Calling OOPS Syntax And Explanation DATA ABSTRACTION Constructor Inheritance 1.Single or simple Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance 5.Hybrid Inheritance Operator Overloading File Operation Python Directory Python Exception Python - Multithreading Python - Database Access Python - CGI Python - Reg Exp Python - Date Python - XML Processing Python - GUI
  • iconPython Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Inheritance

Extending the Properties of one class to another class is known as Inheritance.The main aim of the Inheritance is reusability.
In Python language these inheritances are classified into following types.

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

Single Inheritance
Extending the properties of single parent class to single child class is known as Single or simple Inheritance.
Syntax:
class Parent:
    ...
    ...
    ..
class Child(Parent):
    ...
    ...
    ..
    

Program for Student Details using Single Inhiritance concept
class Parent:
    def  __init__(self):
        self.name=input("Enter  Name")
        self.location=input("Enter Location")
        self.gender=input("Enter Gender")
class Child(Parent):
    def __init__(self):
        Parent.__init__(self)
        self.marks=int(input("Enter Marks"))
    def display_Details(self):
        print("Your Name is",self.name)
        print("Your Location is",self.location)
        print("Your Gender is",self.gender)
        print("Your Marks is",self.marks)

if __name__=="__main__":
    #Create Object For Child Class
    c=Child()
    c.display_Details()    

Output:
OUTPUT:
Enter  Name sateesh
Enter Location chennai
Enter Gender male
Enter Marks 24
 
Your Name Is Sateesh
Your Location Is Chennai
Your Gender Is Male
Your Marks Is 24
MultiLevel Inheritance
Extending the propertis of one class to another class in a Sequential order is known as Multilevel Inheritance.
Syntax:
class A:
    ...
    ...
class B(A):
    ...
    ...
class C(B):
    ...
    ...
class D(C):
    ...
    ...
    

Note:if we create object for the 'D' class we can access A,B,C,D Classes.

Program for Student Details using multilevel Inhiritance concept
class Parent:
    def  __init__(self):
        self.name=input("Enter  Name")
        self.location=input("Enter Location")
        self.gender=input("Enter Gender")
class Child(Parent):
    def  __init__(self):
        Parent.__init__(self)
        self.Address=input("Enter  Address")
    
class Child1(Child):
    def __init__(self):
        Child.__init__(self)
        self.marks=int(input("Enter Marks"))
    def display_Details(self):
        print("Your Name is",self.name)
        print("Your Location is",self.location)
        print("Your Gender is",self.gender)
        print("Your Marks is",self.marks)

if __name__=="__main__":
    #Create Object For Child1 Class
    c=Child1()
    c.display_Details()                                    

Output:
OUTPUT:
Enter  NameSateesh
Enter LocationChennai
Enter GenderMale
Enter  AddressChennai
Enter Marks24

Your Name Is Sateesh
Your Location Is Chennai
Your Gender Is Male
Your Marks Is 24

 
Hierarchical Inheritance
Extending the propertis of Single Parent class to multiple child classes known as Hierarchical Inheritance.
Syntax:
class A:
    ...
    ...
class B(A):
    ...
    ...
class C(A):
    ...
    ...
    
class D(A):
    ...
    ...
     

Program for Student Details using Hirarchy Inhiritance concept

class Parent:
    def  __init__(self):
        self.name=input("Enter  Name")
        self.location=input("Enter Location")
        self.gender=input("Enter Gender")
        self.Address=input("Enter  Address")
           
class Student(Parent):
    def __init__(self):
        Parent.__init__(self)
        self.marks=int(input("Enter Marks"))
    def display_Details(self):
        print("Strudent Details Are ......................")
        print("Your Name is",self.name)
        print("Your Location is",self.location)
        print("Your Gender is",self.gender)
        print("Your Marks is",self.marks)
        print(" ")
class Faculty(Parent):
    def __init__(self):
        Parent.__init__(self)
        self.salary=int(input("Enter Salary"))
    def display_Details(self):
        print("Faculty Details Are ......................")
        print("Your Name is",self.name)
        print("Your Location is",self.location)
        print("Your Gender is",self.gender)
        print("Your Salary is",self.salary)
        print(" ")

class Principal(Parent):
    def __init__(self):
        Parent.__init__(self)
        self.salary=int(input("Enter Salary"))
    def display_Details(self):
        print("Principal Details Are ......................")
        print("Your Name is",self.name)
        print("Your Location is",self.location)
        print("Your Gender is",self.gender)
        print("Your Salary is",self.salary)
        print(" ")

if __name__=="__main__":
    #Create Objects For Child Classes
    s=Student()
    s.display_Details()
    f=Faculty()
    f.display_Details()
    p=Principal()
    p.display_Details()
        
        

Output:
OUTPUT:
Enter  Name Sateesh
Enter Location Chennai
Enter Gender Male
Enter  Address Chennai
Enter Marks 24
Strudent Details Are ......................
Your Name Is Sateesh
Your Location Is Chennai
Your Gender Is Male
Your Marks Is 24
 
Enter  Name Varshini
Enter Location Chennai
Enter Gender Female
Enter  Address Chennai
Enter Salary 24000
Faculty Details Are ......................
Your Name Is Varshini
Your Location Is Chennai
Your Gender Is Female
Your Salary Is 24000
 
Enter  Name Priya
Enter Location CHennai
Enter Gender Female
Enter  Address Chennai
Enter Salary 240000
Principal Details Are ......................
Your Name Is Priya
Your Location Is Chennai
Your Gender Is Female
Your Salary Is 240000

Multiple Inheritance


Extending the propertis of Multiple parent classes to single child class known as Multiple Inheritance
Syntax:

class A:
    ...
    ...
class B:
    ...
    ...
class C:
    ...
    ...
    
class D(A,B,C):
    ...
    ...
     

Program for Student Details using Multiple Inhiritance concept

class Parent1:
    def  __init__(self):
        self.name=input("Enter  Name")
        self.location=input("Enter Location")
        self.gender=input("Enter Gender")
        
           
class Parent2:
    def __init__(self):
        self.Address=input("Enter  Address")
   
class Child(Parent1,Parent2):
    def __init__(self):
        Parent1.__init__(self)
        Parent2.__init__(self)
        self.salary=int(input("Enter Salary"))
    def display_Details(self):
       
        print("Your Name is",self.name)
        print("Your Location is",self.location)
        print("Your Gender is",self.gender)
        print("Your Salary is",self.salary)
        print(" ")




if __name__=="__main__":
    #Create Object For Child Class
    p=Child()
    p.display_Details()
        
        

Output:
OUTPUT:
Enter  Name Varshini
Enter Location Chennai
Enter Gender Female
Enter  Address Chennai
Enter Salary 24000

Your Name Is Varshini
Your Location Is Chennai
Your Gender Is Female
Your Salary Is 24000

Hybrid Inheritance


Combination of above all Inheritances is known as Hybrid Inheritance Syntax:
class A:
    ...
    ...
class B(A):
    ...
    ...
class C:
    ...
    ...
    
class D(A,C):
    ...
    ...
     

Program for Student Details using Hybrid Inhiritance concept

class Parent1:
    def  __init__(self):
        self.name=input("Enter  Name")
        self.location=input("Enter Location")
        self.gender=input("Enter Gender")
        
           
class Parent2:
    def __init__(self):
        self.address=input("Enter  Address")
   
class Child(Parent1,Parent2):
    def __init__(self):
        Parent1.__init__(self)
        Parent2.__init__(self)
        self.salary=int(input("Enter Salary"))
    def display_Details(self):
        print("Child 1 Details")
        print("Your Name is",self.name)
        print("Your Location is",self.location)
        print("Your Gender is",self.gender)
        print("Your Salary is",self.salary)
        print("Your Address is",self.address)
        print(" ")
class Child1(Parent2):
    def __init__(self):
        self.name=input("Enter name")
        Parent2.__init__(self)
    def  display_Details(self):
         print("Child 2 Details")
         print("Your Name is",self.name)
         print("Your Address is",self.address)
        

if __name__=="__main__":
    #Create Object For Child Class
    p=Child()
    p.display_Details()
    c=Child1()
    c.display_Details()
        
        

Output:
OUTPUT:
Enter  Name Sateesh
Enter Location Chennai
Enter Gender Male
Enter  Address Chennai
Enter Salary 24000
Child 1 Details
Your Name Is Sateesh
Your Location Is Chennai
Your Gender Is Male
Your Salary Is 24000
Your Address Is Chennai
 
Enter Name Varshini
Enter  Address Chennai
Child 2 Details
Your Name Is Varshini
Your Address Is Chennai