filmov
tv
Inheritance using parent of base class and child or derived class with multilevel and multiple types
Показать описание
A child or derived class can inherit all the attributes or methods of parent class or base class in Object Oriented Programming.
Base or parent class : Our main class where methods and properties are declared.
Derived or child class: Can use methods and properties of Base class along with its own functionality.
Multilevel Inheritance
There can be multiple level of derived or child classes. One parent class ( y here ) can be the child of another class ( x here ). Class x is the base class of y and y is the base class of z.
X Y Z
With this multilevel of inheritance the object of z can use method of class x.
class x():
def x_one(self):
print("I am x_one")
class y(x):
def y_one(self):
print("I am y_one")
class z(y):
def z_one(self):
print("I am z_one")
z1=z() # object of z class
z1.x_one()
z1.y_one()
z1.z_one()
multiple inheritance
One child class ( z here ) can have multiple parent or base class ( y and x here ). Note that y is not the child class of x but z is the child class of x and y. So z can use methods of both x and y, but y can't use methods of x.
class x():
def x_one(self):
print("I am x_one")
class y():
def y_one(self):
print("I am y_one")
class z(x,y): # z is child of x and y
def z_one(self):
print("I am z_one")
z1=z()
z1.x_one()
z1.y_one()
z1.z_one()
Output
I am x_one
I am y_one
I am z_one
Base or parent class : Our main class where methods and properties are declared.
Derived or child class: Can use methods and properties of Base class along with its own functionality.
Multilevel Inheritance
There can be multiple level of derived or child classes. One parent class ( y here ) can be the child of another class ( x here ). Class x is the base class of y and y is the base class of z.
X Y Z
With this multilevel of inheritance the object of z can use method of class x.
class x():
def x_one(self):
print("I am x_one")
class y(x):
def y_one(self):
print("I am y_one")
class z(y):
def z_one(self):
print("I am z_one")
z1=z() # object of z class
z1.x_one()
z1.y_one()
z1.z_one()
multiple inheritance
One child class ( z here ) can have multiple parent or base class ( y and x here ). Note that y is not the child class of x but z is the child class of x and y. So z can use methods of both x and y, but y can't use methods of x.
class x():
def x_one(self):
print("I am x_one")
class y():
def y_one(self):
print("I am y_one")
class z(x,y): # z is child of x and y
def z_one(self):
print("I am z_one")
z1=z()
z1.x_one()
z1.y_one()
z1.z_one()
Output
I am x_one
I am y_one
I am z_one