Old Class Old Class New Class New Class

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

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:

• The main advantage of inheritance is code reusability.


Why do We need inheritance
class Employee : class Manager :
id = 1 id = 1
@classmethod @classmethod
def getid(cls): def getid(cls):
return cls.id return cls.id
def setname(self, name): def setname(self, name):
self.name = name self.name = name
def getname(self): def getname(self):
return self.name return self.name
def setsalary(self, salary): def setsalary(self, salary):
self.salary = salary self.salary = salary
def getsalary(self): def getsalary(self):
return self.salary return self.salary
def setovertime(self, ot): def setseniorname(self, sname):
self.ot = ot self.sname = sname
def getovertime(self): def getseniorname(self):
return self.ot return self.sname
Parent Class Child Class
class Employee : class Manager :
id = 1 def setsalary(self, salary):
@classmethod
self.salary = salary
def getid(cls):
def getsalary(self):
return cls.id
def setname(self, name): return self.salary
self.name = name def getseniorname(self, sname):
def getname(self): self.sname = sname
return self.name def getseniorname(self):
def setsalary(self, salary): return self.sname
self.salary = salary
def getsalary(self):
return self.salary
def setovertime(self, ot):
self.ot = ot
def getovertime(self):
return self.ot
Type of Inheritance
• Single Inheritance

• Multi-level Inheritance

• Hierarchical Inheritance

• Multiple Inheritance
Declaration of Child Class
class ChildClassName (ParentClassName) :
members of Child class

class Mobile (object) :


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

Father Parent Class

Son Child Class


Syntax:-
class ParentClassName(object): Parent Class
members of Parent Class

class ChildClassName(ParentClassName):
Child Class
members of Child Class

Example:-
class Father: Father
members of class Father Parent Class

class Son (Father):


members of class Son Child Class Son
Inheritance
• We can access Parent Class Variables and Methods using Child Class
Object

• 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

Father Parent Class

Son Child Class

GrandSon GrandChild Class


Syntax:- object
class ParentClassName(object):
members of Parent Class
Parent Class
class ChildClassName(ParentClassName):
members of Child Class
Child Class
class GrandChildClassName(ChildClassName):
members of Grand Child Class
Grand Child
object

class Father (object):


members of class Father Parent Class Father

class Son (Father):


members of class Son Child Class Son

class GrandSon (Son):


members of class GrandSon GrandChild Class GrandSon
Hierarchical Inheritance
object

Father Parent Class

Son Daughter Son

Child Class
Syntax:-
class ParentClassName(object): object
members of Parent Class

Parent Class
class ChildClassName1(ParentClassName):
members of Child Class 2

class ChildClassName2(ParentClassName): Child Class 1 Child Class 2


members of Child Class 2
object
class Father (object):
members of class Father Parent Class

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 Class Parent 1 Parent 2 Parent Class

Child Child Class


Syntax:- object
class ParentClassName1(object):
members of Parent Class

Parent 1 Parent 2
class ParentClassName2(object):
members of Parent Class

class ChildClassName(ParentClassName1, ParentClassName2): Child


members of Child Class
object
class Father (object):
members of class Father 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.

You might also like