1. Single Inheritance – The concept
of inheriting properties from only one class into another class is known as a
single inheritance.
Example –
class A:
def m1(self):
print 'in m1 of A'
class B(A):
def m2(self):
print 'in m2 of B'
B1=B()
B1.m1()
B1.m2()
def m1(self):
print 'in m1 of A'
class B(A):
def m2(self):
print 'in m2 of B'
B1=B()
B1.m1()
B1.m2()
Output –
in m1
of A
in m2 of B
2. Multilevel Inheritance – The concept
of inheriting properties from multiple classes into single class with the
concept of “one after another” is
known as a multilevel inheritance.
Example –
class A:def m1(self):
print 'in m1 of A'
class B(A):
def m2(self):
print 'in m2 of B'
class C(B):
def m3(self):
print 'in m3 of C'
C1=C()
C1.m1()
C1.m2()
C1.m3()
Output –
in m1
of A
in m2
of B
in m3 of C
3. Hierarchical Inheritance – The concept
of inheriting properties from one class into multiple classes is known as
hierarchical inheritance
def m1(self):
print 'in m1 of A'
class B(A):
def m2(self):
print 'in m2 of B'
class C(A):
def m3(self):
print 'in m3 of C'
B1=B()
B1.m1()
B1.m2()
C1=C()
C1.m1()
C1.m3()
Output –
in m1
of A
in m2
of B
in m1
of A
in m3 of C
4. Multiple Inheritances – The concept
of inheriting properties from multiple classes into single class at a time is
known as multiple inheritance.
Example –
class A:
def m1(self):
print 'in m1 of A'
class B:
def m2(self):
print 'in m2 of B'
class C(A,B):
def m3(self):
print 'in m3 of C'
C1=C()
C1.m1()
C1.m2()
C1.m3()
A1=A()
A1.m1()
B1=B()
B1.m2()
def m1(self):
print 'in m1 of A'
class B:
def m2(self):
print 'in m2 of B'
class C(A,B):
def m3(self):
print 'in m3 of C'
C1=C()
C1.m1()
C1.m2()
C1.m3()
A1=A()
A1.m1()
B1=B()
B1.m2()
Output –
in m1
of A
in m2
of B
in m3
of C
in m1
of A
in m2 of B
5. Cyclic Inheritance – The concept
of inheriting the properties from sub class to super class is known as a cyclic
inheritance. Python doesn’t support cyclic inheritance.
Example -
class A(C):
def m1(self):
print 'in m1 of A'
class B(A):
def m2(self):
print 'in m2 of B'
class C(B):
def m3(self):
print 'in m3 of C'
def m1(self):
print 'in m1 of A'
class B(A):
def m2(self):
print 'in m2 of B'
class C(B):
def m3(self):
print 'in m3 of C'
No comments:
Post a Comment