Special Offer - Enroll Now and Get 2 Course at ₹25000/- Only Explore Now!

All Courses
Python Inheritance

Python Inheritance

May 20th, 2019

Python  Inheritance concepts

Inheritance concepts the concepts can be implemented in the programs with suitable programs as an example for each as below . Ability to define a new class that is a modified version of the old class.

Inheritance in Python | GangBoard

Inheritance Types

  • Single inheritance
  • Multiple child class
  • Multiple parent class
  • Multilevel  Inheritance
  • Call parent class constructor form child class

Single Inheritance:

The single inheritance can be achieved by passing  Old Parents class arguments to the child class. 

Program:

Class Parent:
 P_Name=""
  C_Name=""
def show_parent(self):
print(self.  P_Name)
Class child(Parent):
 P_Name=""
 C_Name=""
def show_child(self):
print(self. C_Name)
 arguments
ch_name=child()
ch_name.P_Name="Kumar"
ch_name.C_Name="vijay"
ch_name. show_parent
ch_name.  show_child

Multiple child class  Inheritance:

Two or more child classes can inherit from old parent class.This is called as Multiple child class.

Program:

 Class Parent:
 P_Name=""
C_Name=""
 def show_parent(self):
print(self.  P_Name)
Class cat(Parent):
 def show_child(self):
 print(self. C_Namee)
Class dog(Parent):
def show_child(self):
print(self. C_Name)
 object of cat
ch_A1=cat()
ch_A1.P_Name="cat"
ch_A1.C_Name=" puppy "
ch_A1. show_parent
ch_A1.  show_child
 object of dog
ch_A2=dog()
ch_A2.P_Name="cat"
ch_A2.C_Name=" puppy "
ch_A2. show_parent
ch_A2.  show_child

 Multiple parent class  Inheritance :

In this type of inheritance child class can inherit from a parnet old class.This is called as Multiple parent class  Inheritance.

This Muliple class inheritance can be explained by program as follws.

 Program:
Class mother:
M_Name=""
def show_mother(self):
print(self.  M_Nam)
Class father:
F_Name=""
def show_father(self):
print(self.   F_Name)
Class son(father,mother):
P_Name=""
C_Name=""
def show_parent(self):
print(self. F_Name)
print(self.  M_Name)
object of son
s1=son()
s1.F_Name="cat"
s1.M_Name=" puppy "
s1. show_parent
 

 Multilevel  Inheritance :

In this type of Inheritance, a class can inherit from base class and child classes. This is called as  Multilevel  Inheritance.

Program:

Class Family:
def show_Family(self):
print("This is my Family")
Class mother(Family):
M_Name=""
def show_mother(self):
print(self.  M_Nam)
Class father(Family):
F_Name=""
def show_father(self):
 print(self.   F_Name)
Class son(father,mother):
P_Name=""
C_Name=""
def show_parent(self):
print(self. F_Name)
print(self.  M_Name)
object of son
s1=son()
s1.F_Name="cat"
s1.M_Name=" puppy "
s1. show_ Family
s1.show_parent

 Call parent class constructor form child class Inheritance

 In this type of inheritance, child class constructor initializes the parent class attributes. This is called as Call parent class constructor form child class Inheritance.

 Program:

Class Family:
def __init__(self,name):
self.name=name
Class mother(Family):
M_Name=""
def  __init__(self,name,age):
Family .__init__(self,name)
self.age=age
object of son
MM=mother("aaa",45)
print(MM.name)
print(MM.age)