Old Class Old Class New Class New Class
Old Class Old Class New Class New Class
Old Class Old Class New Class New Class
The mechanism of deriving a new class from an old one (existing class) such that the
new class inherit all the members (variables and methods) of old class is called
inheritance or derivation.
Old Class
New Class
Super Class and Sub Class
The old class is referred to as the Super class and the new one is called the Sub class.
• Parent Class - Base Class or Super Class
• Child Class - Derived Class or Sub Class
Father
• Home
Parent Class • Money
• Business
Son
Child Class •
•
BMW
Job
Inheritance
• All classes in python are built from a single super class called ‘object’ so
whenever we create a class in python, object will become super class for
them internally.
class Mobile(object):
class Mobile:
• Multi-level Inheritance
• Hierarchical Inheritance
• Multiple Inheritance
Declaration of Child Class
class ChildClassName (ParentClassName) :
members of Child class
class Mobile :
members of Child class
Single Inheritance
If a class is derived from one base class (Parent Class), it is called Single
Inheritance.
object
class ChildClassName(ParentClassName):
Child Class
members of Child Class
Example:-
class Father: Father
members of class Father Parent Class
• We can also access Parent Class Variables and Methods using Parent Class
Object
• We can not access Child Class Variables and Methods using Parent Class
Object
Constructor in Inheritance
By default, The constructor in the parent class is available to the child class.
class Father:
def __init__(self):
self.money = 2000
print("Father Class Constructor") What will happen if we define
constructor in both classes ?
s = Son( )
s.disp()
Constructor Overriding
If we write constructor in the both classes, parent class and child class then the
parent class constructor is not available to the child class.
In this case only child class constructor is accessible which means child class
constructor is replacing parent class constructor.
Constructor overriding is used when programmer want to modify the existing
behavior of a constructor.
Constructor Overriding
class Father:
def __init__(self):
self.money = 2000
print("Father Class Constructor")
s = Son()
s.disp()
Constructor with super( ) Method
If we write constructor in the both classes, parent class and child class then the
parent class constructor is not available to the child class.
In this case only child class constructor is accessible which means child class
constructor is replacing parent class constructor.
super ( ) method is used to call parent class constructor or methods from the child
class.
Multi-level Inheritance
In multi-level inheritance, the class inherits the feature of another derived
class (Child Class).
object
Child Class
Syntax:-
class ParentClassName(object): object
members of Parent Class
Parent Class
class ChildClassName1(ParentClassName):
members of Child Class 2
Father
class Son (Father):
members of class Son Child Class
Son Daughter
class Daughter (Father):
members of class Daughter Child Class
Multiple Inheritance
If a class is derived from more than one parent class, then it is called multiple
inheritance.
object
Parent 1 Parent 2
class ParentClassName2(object):
members of Parent Class
Father Mother
class Mother (object):
members of class Mother Parent Class
Son
class Son (Father, Mother):
members of class Son Child Class
Method Resolution Order (MRO)
In the multiple inheritance scenario members of class are searched first in the
current class. If not found, the search continues into parent classes in depth-
first, left to right manner without searching the same class twice.
• Search for the child class before going to its parent class.
• When a class is inherited from several classes, it searches in the order from
left to right in the parent classes.
• It will not visit any class more than once which means a class in the
inheritance hierarchy is traversed only once exactly.
Method Resolution Order (MRO)
s = Son()
• The search will start from Son. As the object of Son is
created, the constructor of Son is called. object
• Son has super().__init__() inside his constructor so its
parent class, the one in the left side ‘Father’ class’s
constructor is called.
• Father class also has super().__init__() inside his
constructor so its parent ‘object’ class’s constructor is Father Mother
called.
• Object does not have any constructor so the search will
continue down to right hand side class (Mother) of object
class so Mother class’s constructor is called. Son
• As Mother class also has super().__inti__() so its parent
class ‘object’ constructor is called but as object class
already visited, the search will stop here.