Method overriding is a feature of any object-oriented programming language that allows a subclass or child class to implement a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, parameters or signature, and return type (or sub-type) as a method in its super-class, the subclass method is said to override the method in the super-class.
To override a function, the following conditions must be met:
The following are some of the key features and benefits of Python method overriding:
# Parent class
class Shape:
# properties
data1 = "abc"
# function no_of_sides
def no_of_sides(self):
print("My sides need to be defined. I am from shape class.")
# function two_dimensional
def two_dimensional(self):
print("I am a 2D object. I am from shape class")
class Square (Shape):
data2 = "XYZ"
def no_of_sides (self):
print("I have 4 sides. I am from Square class")
def color(self):
print("I have teal color. I am from Square class.")
# Create an object of Square class
sq = Square()
# Override the no_of_sides of parent class
sq.no_of_sides()
# Will inherit this method from the parent class
sq.two_dimensional()
# It's own method color
sq.color()
Here,
I have 4 sides. I am from Square class
I am a 2D object. I am from shape class
I have teal color. I am from Square class.
Note: also read about Access Modifiers in Python
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
You are given a stream of elements that is too large to fit into memory.…
The formula for the area of a circle is given by πr². Use the Monte…
Given an integer k and a string s, write a function to determine the length…
There is a staircase with N steps, and you can ascend either 1 step or…
Build an autocomplete system that, given a query string s and a set of possible…
Design a job scheduler that accepts a function f and an integer n. The scheduler…