Advertisement
class derived-class-name(base-class-name):
#member1 of derived class
#member2 of dervied class
.
.
.
# Python - Example of single inheritance
# Base class
class X:
a = 10
b = 20.6
# Method of class X
def method_x(self):
return 'class X'
# Derived class Y, inheriting from the class X
class Y(X):
i = 30
# Method of class Y
def method_y(self):
return 'class Y'
# Creating an object of class Y
ob_y = Y()
# Using the object of class Y to access the inherited member, a, of class X
print('a = : ', ob_y.a)
# Using the object of class Y to access the inherited member, b, of class X
print('b = : ', ob_y.b)
# Using the object of class Y to access the inherited method, method_x() of class X
print(ob_y.method_x())
# Using the object of class Y to access its own member, i.
print('y = : ', ob_y.i)
# Using the object of class Y to access its own method, method_y()
print(ob_y.method_y())
a = : 10
b = : 20.6
class X
y = : 30
class Y
Advertisement
# Python - Example of multiple inheritance
# Base class1
class X:
a = 10
b = 20.6
# Method of class X
def method_x(self):
return 'class X'
# Base class2
class Y:
i = 20
# Method of class Y
def method_y(self):
return 'class Y'
# Derived class Z, inheriting from class X and class Y
class Z(X,Y):
j = 30
# Method of class Z
def method_z(self):
return 'class Z'
# Creating an object of class Z
ob_z = Z()
# Using the object of class Z to access the inherited member, a, of class X
print('a = : ', ob_z.a)
# Using the object of class Z to access the inherited member, b, of class X
print('b = : ', ob_z.b)
# Using the object of class Z to access the inherited member, method_x() of class X
print(ob_z.method_x())
# Using the object of class Z to access the inherited member, i, of class Y
print('i = : ', ob_z.i)
# Using the object of class Z to access the inherited member, method_y() of class Y
print(ob_z.method_y())
# Using the object of class Z to access its own member, j
print('j = : ', ob_z.j)
# Using the object of class Z to access its own member, method_z()
print(ob_z.method_z())
a = : 10
b = : 20.6
class X
i = : 20
class Y
j = : 30
class Z
# Python - Multilevel Inheritance Example
#First Base class
class X:
x1 = 10
def get_x1(self):
return self.x1
# Intermediate base class Y, inheriting from class X
class Y(X):
y1 = 20
def get_y1(self):
return self.y1
# Derived class Z, inheriting the intermediate base class Y
class Z(Y):
z1 = 30
def get_z1(self):
return z1
# Creating an object of derived class
ob = Z()
# Using object of derived class Z, to access an inherited instance variable, x1
print('x1 = : ',ob.x1)
# Using object of derived class Z, to access an inherited method, get_x1()
print('x1 = : ',ob.get_x1())
# Using object of derived class Z, to access an inherited instance variable, y1
print('y1 = : ',ob.y1)
# Using object of derived class Z, to access an inherited method, get_y1()
print('y1 = : ',ob.get_y1())
# Using object of derived class Z, to access its own instance variable, z1
print('z1 = : ',ob.z1)
# Using object of derived class Z, to access its own method, get_z1()
print('z1 = : ',ob.get_z1())
x1 = : 10
x1 = : 10
y1 = : 20
y1 = : 20
z1 = : 30
z1 = : 30
# Python - Example of inheritance
#A general class, Fruit
class Fruit:
def message(self):
print('This is a fruit')
def fruit_name(self):
print('Fruit')
def fruit_color(self):
print('Color')
# A specialized class, Mango inheriting the general class Fruit
class Mango(Fruit):
# Redefining the inherited general method with a specialized version
def fruit_name(self):
print('Name - Mango')
# Redefining the inherited general method with a specialized version
def fruit_color(self):
print('Color - Yellow')
def has_seed(self):
print('Seed - Yes')
# Creating an object of specialized class, Mango
ob= Mango()
# Calling the general method inherited by the specialized class, Mango
ob.message()
# Calling the methods of specialized class, Mango
ob.fruit_name()
ob.fruit_color()
ob.has_seed()
This is a fruit
Name - Mango
Color - Yellow
Seed - Yes
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement