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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…