Inheritance
Inheritance
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
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 ?
class Son (Father):
def disp(self):
print(“Son Class Instance Method:”,self.money)
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")
How can we call parent
class Son(Father): class constructor ?
def __init__(self):
self.money = 5000
print("Son Class Constructor")
def disp(self):
print(self.money)
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):
Child Class
members of class Son
Son Daughter
class Daughter (Father):
Child Class
members of class Daughter
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
Child
class ChildClassName(ParentClassName1, ParentClassName2):
members of Child Class
object
class Father (object):
members of class Father Parent Class
Father Mother
class Mother (object):
Parent Class
members of class Mother
Son