Hello Folks...!!!
Today we are going to discuss the concept of overriding.
Overriding
Consider a simple example. We have 2 classes A and B. B is the child class of
A. So A is base class and B is a derived one. Now we wish both the classes
have a function named print_me1() which prints the class name. But it should
behave differently based on the object/instance with which we call them.
If we create an instance of the parent object and call the print_me1()
function it should print class A, if it is called using the instance of class
B, it should print class B.
This can be done using the concept of overriding.
Overriding allows one to write a new implementation for the function
that is already defined in its parent class.
Example:
Now there are two functions in the base class. The child class overrides only
one function -
print_me1 ( ). So whenever
you call the function name
print_me1() with
an object created for derived class (class B), the method in base class (class
A) will not be executed, the implementation in class B only gets executed.
Thus the method print_me1() in class A can be executed if and only if
you create an instance of class A or you create another derived that inherits
A but does not overrides its method. The function
print_me2() is
not overridden by class B, ie., it does not have any new implementation in the
same function name. So it can be accessed by the instance of the derived class
itself.
Now what if you wish to access the original version in the base class
though overridden method exists ? The super( ) function can be used
!!!!!
super( ) in python
super( )
function is used to create a temporary object for the base class and utilize
its methods. For example let us make slight modification to the example above.
In class B, let us get an option from the user that denotes whether to call
the function in base class or the one in derived class in case of same
function name. Look at the code below.
Now let the user input be 'p', which means call the method in the parent, the
outcome is :
Now you can find the instance of class B is able to call the class A's version
of print_me1().
super( )
function can also be used to just call the constructor of the base without
creating an explicit object for it. For example we look back at the examples
used in
Single Inheritance. Instead of the
class composition, one can use super to just initialize the default values for the variables
in base class. Look at the below.
This is the base class. The class
employee
inherits the class person given above. The modification is made here. Look at
the class employee :
This class, instead of explicitly creating an object and then calling the
constructor, directly calls the base class constructor.
super( ) is used
to call the base class function
read_base( )
also. Now the variables of the base class can be accessed using the self
instance itself. Thus
super( ) function reduces complications.
super( ) function can be used in the way given below also. This just initializes the
base class. In case of multiple inheritance, it allows initialization of all
the parent classes. Look at the example below:
Once initialized, you can access any element of the base class using
self instance
itself.
Next Topic👉
Comments
Post a Comment