How to implement method overriding in python?

Description

To implement method overloading in python3.

Method overriding:

   Overriding is the ability of a class to change the implementation of a method provided by one of its ancestors.

   Through method overriding a class may "copy" another class, avoiding duplicated code.

   Method overriding is thus a strict part of the inheritance mechanism.

   Python method overriding occurs simply defining in the child class a method with the same name of a method in the parent class.

#parent class
class over:
def action(self):
print(‘parent class’)

#child class 1
class ride(over):
def action(self):
print(‘Child class’)

#child class 2
class override(over):
def action(self):
print(‘child class 2′)

#object creation
o = over()
r = ride()

#method calling statement
o.action()
r.action()

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit