Lecture 4
Operator Overloading
1
COURSE INSTRUCTOR
AY S H A S A F D A R
LECTURER COMPUTER SCIENCE H9
CAMPUS
ISLAMABAD
Aysha Safdar, Lecturer CS, NUML H9 camp
us, Islamabad
Operator Overloading
2
Allows a class to define multiple methods with the same
name but varying parameters.
This technique enables developers to perform different
operations based on the type and number of arguments
provided, streamlining code organization and enhancing
its flexibility.
Why Use Overloading in OOP?
Cleaner code
Reduced redundancy
Improved readability. It allows developers to create
intuitive and descriptive method names, making the code
more self-explanatory and easier to maintain.
Aysha Safdar, Lecturer CS, NUML H9 camp
us, Islamabad
Method Overloading
3
In method overloading, a class can have
multiple methods with the same name but
different parameters. When a method is
called, the appropriate version is
automatically selected based on the provided
arguments.
Overloading by Changing Number of
Parameters
Overloading by Using Different Data Types
Aysha Safdar, Lecturer CS, NUML H9 camp
us, Islamabad
Overloading by Changing Number of Parameters
int sum (int a, int b)
{
return a+b;
}
int sum( int a, int b,
int c)
{
return a+b+c;
}
Aysha Safdar, Lecturer CS, NUML H9 camp 4
us, Islamabad
Overloading by using Different Data Types
int sum( int a, int b)
{
return a+b;
}
Double sum(double a, double
b)
{
return a+b;
}
Aysha Safdar, Lecturer CS, NUML H9 camp 5
us, Islamabad
Constructor Overloading
6
constructor overloading enables classes to
have multiple constructors, each accepting
different parameters, facilitating object
initialization based on various scenarios.
Using Different Constructors in a Class
By providing multiple constructors,
developers can create objects with different
initial states, tailoring them to specific needs.
Aysha Safdar, Lecturer CS, NUML H9 camp
us, Islamabad
Constructor overloading
( no parameter, with parameters)
Aysha Safdar, Lecturer CS, NUML H9 camp 7
us, Islamabad
Operator Overloading
8
Operator overloading allows classes to define custom
behaviors for standard operators, such as +, -, *, /, and
more, when applied to class objects.
Overloading Arithmetic Operators
By overloading arithmetic operators, developers can
implement custom mathematical operations for user-
defined objects.
Overloading Comparison Operators
Comparison operator overloading enables developers to
define how objects of a class are compared, providing more
meaningful results for complex data structures.
Aysha Safdar, Lecturer CS, NUML H9 camp
us, Islamabad
Header File
9
Header files contain a set of predefined
standard library functions.
import [Link]
Aysha Safdar, Lecturer CS, NUML H9 camp
us, Islamabad
public class JavaMathExample1
{
public static void main(String[] args)
{
double x = 28;
double y = 4;
// return the maximum of two numbers
[Link]("Maximum number of x and y is: " +[Link](x, y)
// return the square root of y
[Link]("Square root of y is: " + [Link](y));
//returns 28 power of 4 i.e. 28*28*28*28
[Link]("Power of x and y is: " + [Link](x, y));
}
Aysha Safdar, Lecturer CS, NUML H9 camp 10
us, Islamabad