Advertisement
# Python - Members of base class redefined in the derived class.
# Base class
class X:
a = 10
b = 20.6
# Method of class X
def message(self):
print('class X')
# Derived class Y, inherited from class X
class Y(X):
b = 20
i = 20
# Method of class Y
def message(self):
print('class Y')
# Creating an object of class X
ob_x = X()
# Using the object of class X to access its member - instance variable, a
print('a = : ', ob_x.a)
# Using the object of class X to access its member - instance variable, b
print('b = : ', ob_x.b)
# Using the object of class X to access its member, method - message()
ob_x.message()
# 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,
# which is now redefined in class Z with the same name.
print('b = : ', ob_y.b)
# Using the object of class Y to call the inherited method, message() of class X
# which is now redefined in class Y with the same name, it will call the definition of message() in class Y
ob_y.message()
# Using the object of class Y to access its own member, i.
print('i = : ', ob_y.i)
a = : 10
b = : 20.6
class X
a = : 10
b = : 20
class Y
i = : 20
Advertisement
# Python - Using super() function to access a member of base class redefined in the derived class.
# Base class
class X:
a = 10
b = 20.6
# Method of class X
def message(self):
print('class X')
# Derived class
class Y(X):
b = 20
i = 20
# Method of class Y
def message(self):
print('class Y')
# Using the super() function to access the method, message() of base class X
super().message()
# Using the super() function to access the member, b of class X
print('b = : ', super().b)
# 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,
# which is now redefined in class Z with the same name.
print('b = : ', ob_y.b)
# Using the object of class Y to access its own member, i.
print('i = : ', ob_y.i)
# Using the object of class Y to call the inherited method, message() of class X
# which is now redefined in class Y with the same name, it will call the definition of message() in class Y
ob_y.message()
a = : 10
b = : 20
i = : 20
class Y
class X
b : 20.6
class-name.class-member
# Python - Members of base class redefined in the derived class.
# Base class
class X:
a = 10
b = 20.6
# Method of class X
def message(self):
print('class X')
# Derives class
class Y(X):
b = 20
i = 20
# Method of class Y
def message(self):
print('class Y')
# Using the fully qualified class name of class X, to access its method, message()
X.message(self)
# Using the fully qualified class name of class X, to access its member, x.
print('b : ', X.b)
# 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,
# which is now redefined in class Z with the same name.
print('b = : ', ob_y.b)
# Using the object of class Y to access its own member, i.
print('i = : ', ob_y.i)
# Using the object of class Y to call the inherited method, message() of class X
# which is now redefined in class Y with the same name, it will call the definition of message() in class Y
ob_y.message()
a = : 10
b = : 20
i = : 20
class Y
class X
b : 20.6
# Python - Using super() function to call the constructor of base class from the derived class.
# Base class
class X:
# Constructor of X
def __init__(self):
print('Constructor of X')
# Derived class
class Y(X):
# Constructor of Y
def __init__(self):
print('Constructor of Y')
# Using super() to call the constructor of the base class, X
super().__init__();
# Creating an object of class Y
ob_y = Y()
Constructor of Y
Constructor of X
# Python - Using super() function to call the constructor of a base class from a method of its derived class.
# Base class
class X:
# Constructor of X
def __init__(self):
print('Constructor of X')
# Derived class
class Y(X):
# Constructor of Y
def __init__(self):
print('Constructor of Y')
# Method of class Y
def message(self):
print('The message() method of Y')
# Using the super() function to call the base class constructor
super().__init__();
# Creating an object of class Y
ob_y = Y()
# Using the object of Y to call its method, message()
ob_y.message()
Constructor of Y
The message() method of Y
Constructor of X
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement