0% found this document useful (0 votes)
9 views12 pages

Python Operator and Method Overloading

The document outlines a practical exercise for students on advanced Python programming, focusing on operator overloading, method overloading, and method overriding. It details learning objectives, prior knowledge required, and the concepts of compile-time and run-time polymorphism. Additionally, it includes programming tasks that require students to implement various classes and methods demonstrating these concepts.

Uploaded by

bnchavan519
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

Python Operator and Method Overloading

The document outlines a practical exercise for students on advanced Python programming, focusing on operator overloading, method overloading, and method overriding. It details learning objectives, prior knowledge required, and the concepts of compile-time and run-time polymorphism. Additionally, it includes programming tasks that require students to implement various classes and methods demonstrating these concepts.

Uploaded by

bnchavan519
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Smt.

Chandibai Himathmal Mansukhani College

SEC: Advanced Python Programming


Practical 05: Operator Overloading, Method Overloading and Method
Overriding

Learning Objectives

Students will be able to:

Content:
➢ Understand the concepts of method overloading and method overriding in Python.
➢ Differentiate between compile-time (overloading) and runtime (overriding) polymorphism.
➢ Implement method overloading using default arguments and variable-length arguments.
➢ Override methods in derived classes to modify or extend functionality.
➢ Use operator overloading to redefine built-in operators (+, -, *, ==, etc.) for custom objects.
Process:
➢ Develop Python programs demonstrating method overloading and overriding.
➢ Apply polymorphism in object-oriented programming to enhance code reusability.
➢ Overload operators to customize behavior for user-defined classes (e.g., adding two objects of
a class).
Prior Knowledge:
➢ Familiarity with Python classes, objects, and inheritance.

Polymorphism in Object-Oriented Programming


Polymorphism is a fundamental concept in Object-Oriented Programming (OOP) that
allows a single interface to be used for different data types. It enables objects of different classes to be
treated as objects of a common super class, allowing flexibility and scalability in software development.
Types of Polymorphism in Python:
1️. Compile-Time Polymorphism (Method Overloading)
• Definition: It allows multiple methods with the same name but different parameters within a class.
• Implementation in Python: Python does not support traditional method overloading like other
languages (e.g., Java, C++), but it can be achieved using default arguments or variable-length
arguments (*args, **kwargs).
2. Run-Time Polymorphism (Method Overriding)
• Definition: When a method in a child class has the same name as a method in the parent class but
provides a different implementation.
• Purpose: It helps modify or extend the behavior of inherited methods in a subclass.
3. Operator Overloading
• Definition: Allows operators like +, -, *, ==, etc., to be redefined for user-defined classes using
special methods (dunder methods).
• Purpose: Enhances the usability of objects by enabling arithmetic and comparison operations
between them.

Batch No:___________/ Roll No:_____________ 67


[Link] Himathmal Mansukhani College

Q1. Write a Python program to overload the + operator for adding two complex
numbers using a class.

Source Code :

Output:

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 68


[Link] Himathmal Mansukhani College
Q2. Implement a class Shape with a method area() that calculates the area of a square
(if one argument is passed) and a rectangle (if two arguments are passed).

Source Code:

Output:

Batch No:___________/ Roll No:_____________ 69


[Link] Himathmal Mansukhani College
Q3. Write a Python class Calculator with a method add() that supports both two and
three arguments using method overloading.
a) Using Default Arguments
b) Using *args (Variable Arguments)
Source Code a) :

Output:

Source Code b):

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 70


[Link] Himathmal Mansukhani College

Output:

Q4. Define a class Employee with a method display_info() that displays employee
details. Overload this method to support optional parameters like salary and
department.
Source Code:

Batch No:___________/ Roll No:_____________ 71


[Link] Himathmal Mansukhani College

Output:

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 72


[Link] Himathmal Mansukhani College
Q5. Implement a method volume() in a Box class that calculates volume for different
shapes (cube, rectangular box) based on the number of parameters.
Source Code:

Output:

Batch No:___________/ Roll No:_____________ 73


[Link] Himathmal Mansukhani College
Q6. Create a base class Payment with a method process_payment(amount). Then,
create two derived classes:

CreditCardPayment that overrides process_payment(amount) to print "Processing credit


card payment of amount: <amount>".
UPIPayment that overrides process_payment(amount) to print "Processing UPI payment
of amount: <amount>".
Write a program that takes an amount and payment method as input, creates the
corresponding payment object, and calls the process_payment(amount) method.
Source Code:

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 74


[Link] Himathmal Mansukhani College
Output:

Q7. Write a program where a parent class Employee has a method calculate_salary(),
and a subclass Manager overrides it with a different calculation.

Source Code:

Batch No:___________/ Roll No:_____________ 75


[Link] Himathmal Mansukhani College

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 76


[Link] Himathmal Mansukhani College
Output:

Practical Time: ________________________________

Reported At: ________________________________

Date: ________________________ Teacher’s Signature: _________________________

Batch No:___________/ Roll No:_____________ 77


[Link] Himathmal Mansukhani College
Notes:

Smt. CHM College/CS Dept./Sem-II/SEC/Adv. Python Programming/2024-2025 78

You might also like