Control statement
Conditional
Iterative statements Transfer statements
statements
break
if
for continue
if-elif
while pass
If-elif-else
return
Different types of Inheritance
Single Level Inheritance
Example:
Parent
class Parent:
def method1(self):
print("parent method")
Child
class Child(Parent):
def method2(self):
print("child method")
c = Child()
c.method1() # Parent class will be available in child class also
c.method2()
Different types of Inheritance
Example:
class Parent:
def method1(self):
Multi Level Inheritance print("parent method")
Parent class Child(Parent):
def method2(self):
print("child method")
class ChildChild(Child):
Child def method3(self):
print(“child child method")
Child c = ChildChild()
Child c.method1() # Grand Parent class will be available in child class also
c.method2() #Parent class
c.method3()
Different types of Inheritance
Deriving multiple child classes from
Parent class
Hierarchical Inheritance
class Parent:
def method1(self):
Parent print("parent method")
class Child1(Parent):
Child 2 def method2(self):
Child1 Child 3
print("child1 method")
class Child2(Parent):
The methods present in the def method3(self):
parent class is accessible from any print(“child2 method")
of the child class object but one
child class object cannot access c1 = Child1()
other child class method c2 = Child2()
c1.method1() # Parent class will be available in child
class also
c1.method2()
c2.method1() #parent class method
c2.method3()
Different types of Inheritance
Child is derived from 2 parent classes
Multiple Inheritance
class Parent1:
def method1(self):
print("parent 1 method")
Parent1 Parent2 class Parent2:
def method2(self):
print(“parent 2 method")
Child class Child(Parent1, Parent2):
def method3(self):
print(“child method")
c = Child()
c.method1() # parent1 method can be accessed
c.method2() # parent2 method can be access
c.method3() # child class method
Different types of Inheritance
Hybrid Inheritance is a combination of Single, Multi Level, Multiple and Hierarchical
inheritance
A B
G
F
Method Resolution OrderMRO(C) = C + Merge(MRO(A),
MRO(B), AB)
= C +Merge(A, Object, B,
Object, AB)
= C + A + Merge(Object, B,
Object, B)
= C + A + B + Merge(Object,
Object)
=C+A+B
Method Resolution Order
2nd example:
MRO(A) = A, object
MRO(B)= B, object
MRO(C) = C, A, object
MRO(D) = D, A, B, object
MRO(E) = E, B, object
MRO(F) = F + Merge(MRO(C), MRO(D), MRO(E),CDE)
= F+Merge(C, A, object,D, A, B, object,E, B, object, CDE)
= F+C + Merge(A, D, A, B, ,E, B, DE)
= F+C + A + Merge(D,B,E, B, DE)
= F+C+A+D+Merge(B,E,B,E)
F+C+A+D+B+Merge(E,E)
F+C+A+D+B+E
Method Resolution Order
Object mro(A) = A,object
mro(B) = B,object
mro(C) = C,object
mro(D) = {D, A, B, object}
A B C mro(E) = { E, A, C, object}
mro(F)= F+merge(mro(D), mro(E), DE)
= F + merge(D,A,B,E,A,C,DE)
= F + D + merge(A,B, E, A, C, E)
= F+D+A+merge(B,E,C,E)
D E = F+D+A+B+merge(E,C, E)
= F+D+A+B+E+merge(C)
= F+D+A+B+E+C
F
Method Resolution Order
Object mro(A) = A,object
mro(B) = B,object
mro(C) = C,object
mro(D) = {D, A, B, object}
A B C mro(E) = { E, A, C, object}
mro(F)= F + merge(mro(D), mro(E),
mro(C), DEC)
= F + merge(D, A, B,E, A, C,C,DEC)
E = F + D + merge(A,B,E,A,C, C, EC)
D = F+D+A+merge(B, E,C, C, EC)
= F+D+A+B+merge(E,C,C,EC)
= F+D+A+B+E+merge(C,C,C)
= F+D+A+B+E+C
F