CS3391 – OBJECT ORIENTED
PROGRAMMING
UNIT II
LECTURE 11
OVERLOADING METHODS – OBJECTS AS
PARAMETERS – RETURNING OBJECTS
By
Mrs. S. Rajathi
AP / CSE
AAACET-DEPT. OF CSE 1 CS3391 - OOP
SYLLABUS
UNIT II
INHERITANCE, PACKAGES AND INTERFACES
Overloading Methods – Objects as Parameters – Returning
Objects – Static, Nested and Inner Classes. Inheritance:
Basics– Types of Inheritance -Super keyword -Method
Overriding – Dynamic Method Dispatch –Abstract Classes
– final with Inheritance. Packages and Interfaces:
Packages – Packages and Member Access –Importing
Packages – Interfaces.
Content Beyond Syllabus : Legacy Classes and Interfaces
AAACET-DEPT. OF CSE 2 CS3391 - OOP
OVERLOADING METHODS
• Method Overloading allows different methods to have
the same name, but different signatures where the
signature can differ by the number of input parameters
or type of input parameters, or a mixture of both.
• Method overloading is also known as Compile-time
Polymorphism, Static Polymorphism, or Early binding in
Java.
AAACET-DEPT. OF CSE 3 CS3391 - OOP
ADVANTAGES OF METHOD OVERLOADING
• Method overloading improves the Readability and
reusability of the program.
• Method overloading reduces the complexity of the
program.
• Using method overloading, programmers can perform a
task efficiently and effectively.
• Using method overloading, it is possible to access
methods performing related functions with slightly
different arguments and types.
• Objects of a class can also be initialized in different
ways using the constructors.
AAACET-DEPT. OF CSE 4 CS3391 - OOP
DIFFERENT WAYS OF METHOD OVERLOADING IN
JAVA
• Changing the Number of Parameters
• Changing Data Types of the Parameters
• Changing the both
AAACET-DEPT. OF CSE 5 CS3391 - OOP
OVERLOADING METHODS – EXAMPLE PROGRAM
(Changing the Number of Parameters)
// Java program to demonstrate working of method
// overloading in Java
public class Sum {
// Overloaded sum(). This sum takes one int parameter
public int sum(int x)
{
return (x + 100);
}
//Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
AAACET-DEPT. OF CSE 6 CS3391 - OOP
OVERLOADING METHODS – EXAMPLE PROGRAM
//Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
} Output :
public static void main(String args[]) 110
{ 30
Sum s = new Sum(); 60
System.out.println(s.sum(10));
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
}
}
AAACET-DEPT. OF CSE 7 CS3391 - OOP
OVERLOADING METHODS – EXAMPLE PROGRAM
(Changing the type of Parameters)
// Java program to demonstrate working of method
// overloading in Java
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
//Overloaded sum(). This sum takes two float parameters
public float sum(float x, float y)
{
return (x + y);
}
AAACET-DEPT. OF CSE 8 CS3391 - OOP
OVERLOADING METHODS – EXAMPLE PROGRAM
//Overloaded sum(). This sum takes one int & one float
parameters
public float sum(int x, float y)
{
return (x + y); Output :
}
public static void main(String args[]) 30
{ 30.5
Sum s = new Sum(); 31
System.out.println(s.sum(10,20));
System.out.println(s.sum(10,20.5));
System.out.println(s.sum(10.5, 20.5));
}
}
AAACET-DEPT. OF CSE 9 CS3391 - OOP
OVERLOADING METHODS – EXAMPLE PROGRAM
(Changing the both)
// Java program to demonstrate working of method
// overloading in Java
public class Sum {
// Overloaded sum(). This sum takes two int parameters
public int sum(int x, int y)
{
return (x + y);
}
//Overloaded sum(). This sum takes three int parameters
public int sum(int x, int y, int z)
{
return (x + y + z);
}
AAACET-DEPT. OF CSE 10 CS3391 - OOP
OVERLOADING METHODS – EXAMPLE PROGRAM
// Overloaded sum(). This sum takes two double
// parameters
public double sum(double x, double y)
{
return (x + y); Output :
}
public static void main(String args[]) 30
{ 60
Sum s = new Sum(); 31.0
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}
AAACET-DEPT. OF CSE 11 CS3391 - OOP
OBJECTS AS PARAMETERS & RETURNING OBJECTS
• In Java, similar to passing variables to methods, objects
of a class can be passed as argument to a method. The
type of the argument is specified as the name of the
class to which the object belongs.
• A method can return an object in a similar manner as
that of returning a variable of primitive types from
methods. When a method returns an object, the return
type of the method is the name of the class to which the
object belongs and the normal return statement in the
method is used to return the object.
AAACET-DEPT. OF CSE 12 CS3391 - OOP
OBJECTS AS PARAMETERS & RETURNING OBJECTS
– EXAMPLE PROGRAM
//Add two times and display in proper time format
import java.util.Scanner;
class Time
{
private int hours, mins, secs;
Time() //Constructor with no parameter
{
hours = mins = secs = 0;
}
Time(int h, int m, int s) //Constructor with 3 parameters
{
hours = h;
mins = m;
secs = s;
}
AAACET-DEPT. OF CSE 13 CS3391 - OOP
OBJECTS AS PARAMETERS & RETURNING OBJECTS
– EXAMPLE PROGRAM
public Time add(Time tt2)
{
Time Temp = new Time();
Temp.secs = secs + tt2.secs ; //add seconds
Temp.mins = mins + tt2.mins; //add minutes
Temp.hours = hours + tt2.hours; //add hours
if(Temp.secs > 59) //seconds overflow check
{
Temp.secs = Temp.secs - 60; //adjustment of seconds
Temp.mins++; //carry minutes
}
AAACET-DEPT. OF CSE 14 CS3391 - OOP
OBJECTS AS PARAMETERS & RETURNING OBJECTS
– EXAMPLE PROGRAM
if(Temp.mins > 59) //minutes overflow check
{
Temp.mins = Temp.mins - 60; //adjustment of minutes
Temp.hours++; //carry hour
}
return Temp;
}
public void display()
{
System.out.println(hours + " : "+ mins +" : "+ secs);
}
}
AAACET-DEPT. OF CSE 15 CS3391 - OOP
OBJECTS AS PARAMETERS & RETURNING OBJECTS
class ReturningObjectsfromMethods
{
public static void main(String[] args)
{
int h,m,s;
Scanner get = new Scanner(System.in);
h = get.nextInt();
m = get.nextInt();
s = get.nextInt();
Time t1 = new Time(h,m,s);
h = get.nextInt();
m = get.nextInt();
s = get.nextInt();
Time t2 = new Time(h,m,s);
AAACET-DEPT. OF CSE 16 CS3391 - OOP
OBJECTS AS PARAMETERS & RETURNING OBJECTS
– EXAMPLE PROGRAM
Time t3;
t3 = t1.add(t2);
System.out.print("Time after addition (hh:min:ss) ---- >");
t3.display();
}
}
Output :
4 58 55
3 20 10
Time after addition (hh:min:ss) ---->8 : 19 : 5
AAACET-DEPT. OF CSE 17 CS3391 - OOP
QUESTION BANK
PART – A
1 Define method overloading. RE, CO2
2 Write the advantages of method overloading. RE, CO2
Write the code for overload a method display() to
3 AP, CO2
display int, float and string arguments.
How to specify the return type if we return object
4 UN, CO2
from a method?
PART – B
1 Explain method overloading with an example. UN, CO2
Describe in detail passing object as argument to
UN, CO2
2 method and return object from method with an
example program.
AAACET-DEPT.
September 17, 2022
OF CSE 18 CS3391 - OOP
18
THANK YOU
AAACET-DEPT. OF CSE 19 CS3391 - OOP