1. Inheritance isthe capabilityof one classtoderive orinheritthe propertiesfromanotherclass.The benefitsof
inheritance are:
It representsreal-worldrelationshipswell.
It providesreusabilityof acode.We don’thave to write the same code againand again.Also,itallowsusto add
more featurestoa class withoutmodifyingit.
It istransitive innature,whichmeansthatif classB inheritsfromanotherclassA,thenall the subclassesof B would
automaticallyinheritfromclassA.
# A Pythonprogram to demonstrate inheritance
# Base or Superclass.Note objectinbracket.
# (Generally,objectismade ancestorof all classes)
# InPython3.x "classPerson"is
# equivalentto"classPerson(object)"
classPerson(object):
# Constructor
def __init__(self,name):
self.name=name
# To get name
def getName(self):
returnself.name
# To checkif thispersonisan employee
def isEmployee(self):
returnFalse
# InheritedorSubclass(Note Personinbracket)
classEmployee(Person):
# Here we returntrue
def isEmployee(self):
returnTrue
# Drivercode
emp= Person("Geek1") # AnObjectof Person
print(emp.getName(),emp.isEmployee())
emp= Employee("Geek2") #An Objectof Employee
print(emp.getName(),emp.isEmployee())
2. ==============
Subclassing(Callingconstructorof parentclass)
A childclassneedstoidentifywhichclassisitsparentclass.Thiscan be done bymentioningthe parent classname in
the definitionof the childclass.
Eg: class subclass_name (superclass_name):
_ _ _
_ _ _
# Pythoncode to demonstrate howparentconstructors
# are called.
# parentclass
classPerson( object):
# __init__ isknownas the constructor
def __init__(self,name,idnumber):
self.name =name
self.idnumber=idnumber
def display(self):
print(self.name)
print(self.idnumber)
# childclass
classEmployee( Person):
def __init__(self,name,idnumber,salary,post):
self.salary=salary
self.post=post
# invokingthe __init__of the parentclass
Person.__init__(self,name,idnumber)
# creationof an objectvariable oran instance
a = Employee('Rahul',886012, 200000, "Intern")
# callingafunctionof the class Personusingitsinstance
a.display()
====
Differentformsof Inheritance:
1. Single inheritance:Whenachildclassinheritsfromonlyone parentclass,itiscalledsingle inheritance.We sawan
example above.
3. 2. Multiple inheritance:Whenachildclassinheritsfrommultipleparentclasses,itiscalledmultiple inheritance.
Unlike Javaand like C++,Pythonsupportsmultipleinheritance.We specifyall parentclassesasacomma-separated
listinthe bracket.
# Pythonexampletoshowthe workingof multiple
# inheritance
classBase1(object):
def __init__(self):
self.str1="Geek1"
print("Base1")
classBase2(object):
def __init__(self):
self.str2="Geek2"
print("Base2")
classDerived(Base1,Base2):
def __init__(self):
# Callingconstructorsof Base1
# and Base2 classes
Base1.__init__(self)
Base2.__init__(self)
print("Derived")
def printStrs(self):
print(self.str1,self.str2)
ob = Derived()
ob.printStrs()
=======
3. Multilevel inheritance:Whenwe have achildand grandchild relationship.
# A Pythonprogramto demonstrate inheritance
# Base or Superclass.Note objectinbracket.
# (Generally,objectismade ancestorof all classes)
# InPython3.x "classPerson"is
# equivalentto"classPerson(object)"
classBase(object):
# Constructor
4. def __init__(self,name):
self.name=name
# To get name
def getName(self):
returnself.name
# InheritedorSubclass(Note Personinbracket)
classChild(Base):
# Constructor
def __init__(self,name,age):
Base.__init__(self,name)
self.age =age
# To get name
def getAge(self):
returnself.age
# InheritedorSubclass(Note Personinbracket)
classGrandChild(Child):
# Constructor
def __init__(self,name,age,address):
Child.__init__(self,name,age)
self.address=address
# To get address
def getAddress(self):
returnself.address
# Drivercode
g = GrandChild("Geek1",23,"Noida")
print(g.getName(), g.getAge(),g.getAddress())