- The features of all the base classes are inherited into the derived class We can inherit a derived class from another derived class, this process is known as multilevel inheritance.
- This is an example of a child and grandchild relationship.
- Multi-level inheritance is archived when a derived class inherits another derived class.
- There is no limit on the number of levels up to which, the multi-level inheritance is archived in python.
- when we call a method using derived class and if that method is not available in that derived class it will check it in its parent class. if that method is not present in parent class then it will search it from a grandparent.
- Create a Parent Class A.
class A:
2.Create Class B and inherit it from class A
class B(A):
3.Create Class C and inherit from class B
class C(B):
-
Repeat this process until you Required.
-
Create an object of last derived class and access methods and attributes of all the above parent and grandparent class.
#Parent Class For B
class <A>:
#initilize attributes and methods
#Child Class for Class A And Parent class For Class C
class <B>(A):
#initilize class b cand call constructor of class A
#Child class for B
class <C>(B):
#call the constructor of class B
#Access here All the properties of class A and Class B
#Create an object of C Class
c1=C()
class A:
def __init__(self):
self.Avar="I am A Class variable"
class B(A):
def __init__(self):
A.__init__(self)
self.Bvar="I am B Class variable"
class C(B):
def __init__(self):
B.__init__(self)
print(self.Avar)
b1=C()
Output:
I am A Class variable
#Parent Class A
class A:
def __init__(self):
self.Avar="I am A Class variable"
self.a=10
#Child class B
class B(A):
def __init__(self):
#Call the parent class A Constructor
A.__init__(self)
#initilize Instance variables for B class
self.Bvar="I am B Class variable"
self.a=20
class C(B):
def __init__(self):
#Call the parent class B Constructor
B.__init__(self)
print('---------------------In Class C----------------------------')
#Read Variable from class A
print("--------------Class A variable--------------\n")
print(self.Avar)
print("\n--------------Class B variable--------------\n")
#Read Variable From Class B
print(self.Bvar)
print('\n---------The variable from first parent class---------\n')
#Print a value
print(self.a)
#create object of C class
b1=C()
Output
#Result
---------------------In Class C----------------------------
--------------Class A variable--------------
I am A Class variable
--------------Class B variable--------------
I am B Class variable
---------The variable from first parent class---------
20