Method Overriding in Python
Method Overriding in Python
The method overriding in Python means creating two methods with the same
name but differ in the programming logic. The concept of Method overriding
allows us to change or override the Parent Class function in the Child Class.
You can’t override a method within the same class. It means you
have to do it in the child class using the Inheritance concept.
To override the Parent Class method, you have to create a function
in the Child with the same name and the same number of
parameters.
For example, if you have the function that calculates the Salary Hike for all the
employees in an Organisation. But particular departments or people get a
different percentage. In this scenario, you can override the existing method in the
Department class and write your logic.
class Employee:
def message(self):
class Department(Employee):
def message(self):
emp = Employee()
emp.message()
print('------------')
dept = Department()
dept.message()
------------
As you can see, the emp object is printing a string from the Employee message
function. Whereas dept.message() is a printing test from Department.
In this Method Overriding example, we created one more class that was inherited
from the Employee. Next, we are also overriding the message function from this
Sales class. Please refer class example.
class Employee:
def message(self):
class Department(Employee):
def message(self):
class Sales(Employee):
def message(self):
emp = Employee()
emp.message()
print('------------')
dept = Department()
dept.message()
print('------------')
sl = Sales()
sl.message()
------------
------------
Refer to the Inheritance in Python.
class Employee:
def message(self):
class Department(Employee):
def message(self):
print('This Department is inherited from Employee')
class Sales(Department):
def message(self):
emp = Employee()
emp.message()
print('------------')
dept = Department()
dept.message()
print('------------')
sl = Sales()
sl.message()
------------
with arguments
Until now, we are only changing the print statement. I mean, overriding functions
without any arguments. In this example, we created an add function with two
arguments in the parent and three arguments in the child class.
class Employee:
class Department(Employee):
emp = Employee()
emp.add(10, 20)
print('------------')
dept = Department()
class Employee:
def message(self):
class Department(Employee):
def message(self):
Employee.message(self)
emp = Employee()
emp.message()
print('------------')
dept = Department()
dept.message()
------------
From the above screenshot, both give different results based on the object. Here,
Employee.message(self) calls the print(‘This message is from Employee’)
statement and prints the message within that print function. Instead of using the
class name, you can use the super() function to call the parent method.
class Employee:
def message(self):
class Department(Employee):
def message(self):
super().message()
emp = Employee()
emp.message()
print('------------')
dept = Department()
dept.message()
Related Topics
Arithmetic Operators
Assignment
Bitwise
Comparison
Logical
If Statement
If Else
elif Statement
Nested If
For Loop
while Loop
break Statement
continue
Dictionary
datetime
String
Set Object
Tuple Object
List Object
List Comprehensions
lambda
Functions Introduction
Types of Funcs
Iterator
File Handling
Directory
Class
classmethod
Inheritance
Method Overriding
static method
Connect to SQL Server
len method
max method
map method
print method
sort method
range
zip method
math methods
String methods
List methods
NumPy Array
np random array
np concatenate
np Array shape
pandas DataFrame
DataFrame plot
Series
matplotlib Histogram
matplotlib Scatter Plot
matplotlib Pie Chart
matplotlib Bar Chart
List length
List sort
String Concatenation
String Length
substring
split String
String format
String replace
Program Examples