Method Overloading in Java With Examples
Method Overloading in Java With Examples
Method Overloading is a feature that allows a class to have more than one method having the same
name, if their argument lists are different. It is similar to constructor overloading in Java, that allows
a class to have more than one constructor having different argument lists.
let’s get back to the point, when I say argument list it means the parameters that a method has: For
example the argument list of a method add(int a, int b) having two parameters is different from the
argument list of the method add(int a, int b, int c) having three parameters.
add(int, int)
add(int, int, int)
2. Data type of parameters.
For example:
add(int, int)
add(int, float)
add(int, float)
add(float, int)
Method overloading is an example of Static Polymorphism. We will discuss polymorphism and types
of it in a separate tutorial.
Points to Note:
1. Static Polymorphism is also known as compile time binding or early binding.
2. Static binding happens at compile time. Method overloading is an example of static binding
where binding of method call to its de nition happens at Compile time.
class DisplayOverloading
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(char c, int num)
{
System.out.println(c + " "+num);
}
}
class Sample
{
public static void main(String args[])
{
DisplayOverloading obj = new DisplayOverloading();
obj.disp('a');
obj.disp('a',10);
}
}
Output:
a
a 10
In the above example – method disp() is overloaded based on the number of parameters – We have
two methods with the name disp but the parameters they have are different. Both are having
different number of parameters.
class DisplayOverloading2
{
public void disp(char c)
{
System.out.println(c);
}
public void disp(int c)
{
System.out.println(c );
}
}
class Sample2
{
public static void main(String args[])
{
DisplayOverloading2 obj = new DisplayOverloading2();
obj.disp('a');
obj.disp(5);
}
}
Output:
a
5
class DisplayOverloading3
{
public void disp(char c, int num)
{
System.out.println("I’m the first definition of method disp");
}
public void disp(int num, char c)
{
System.out.println("I’m the second definition of method disp" );
}
}
class Sample3
{
public static void main(String args[])
{
DisplayOverloading3 obj = new DisplayOverloading3();
obj.disp('x', 51 );
obj.disp(52, 'y');
}
}
Output:
class Demo{
void disp(int a, double b){
System.out.println("Method A");
}
void disp(int a, double b, double c){
System.out.println("Method B");
}
public static void main(String args[]){
Demo obj = new Demo();
/* I am passing float value as a second argument but
* it got promoted to the type double, because there
* wasn't any method having arg list as (int, float)
*/
obj.disp(100, 20.67f);
}
}
Output:
Method A
As you can see that I have passed the oat value while calling the disp() method but it got
promoted to the double type as there wasn’t any method with argument list as (int, oat)
But this type promotion doesn’t always happen, lets see another example:
class Demo{
void disp(int a, double b){
System.out.println("Method A");
}
void disp(int a, double b, double c){
System.out.println("Method B");
}
void disp(int a, float b){
System.out.println("Method C");
}
public static void main(String args[]){
Demo obj = new Demo();
/* This time promotion won't happen as there is
* a method with arg list as (int, float)
*/
obj.disp(100, 20.67f);
}
}
Output:
Method C
As you see that this time type promotion didn’t happen because there was a method with matching
argument type.
Type Promotion table:
The data type on the left side can be promoted to the any of the data type present in the right side
of it.
Result: Compile time error. Argument lists are exactly same. Both methods are having same number,
data types and same sequence of data types.
Case 2:
Result: Perfectly ne. Valid case of overloading. Here data types of arguments are different.
Case 3:
Result: Perfectly ne. Valid case of overloading. Here number of arguments are different.
Case 4:
Result: Perfectly ne. Valid case of overloading. Sequence of the data types of parameters are
different, rst method is having (int, oat) and second is having ( oat, int).
Case 5:
class Demo
{
public int myMethod(int num1, int num2)
{
System.out.println("First myMethod of class Demo");
return num1+num2;
}
public int myMethod(int var1, int var2)
{
System.out.println("Second myMethod of class Demo");
return var1-var2;
}
}
class Sample4
{
public static void main(String args[])
{
Demo obj1= new Demo();
obj1.myMethod(10,10);
obj1.myMethod(20,12);
}
}
Answer:
It will throw a compilation error: More than one method with same name and argument list cannot
be de ned in a same class.
Question 2 – return type is different. Method name & argument list same.
class Demo2
{
public double myMethod(int num1, int num2)
{
System.out.println("First myMethod of class Demo");
return num1+num2;
}
public int myMethod(int var1, int var2)
{
System.out.println("Second myMethod of class Demo");
return var1-var2;
}
}
class Sample5
{
public static void main(String args[])
{
Demo2 obj2= new Demo2();
obj2.myMethod(10,10);
obj2.myMethod(20,12);
}
}
Answer:
It will throw a compilation error: More than one method with same name and argument list cannot
be given in a class even though their return type is different. Method return type doesn’t matter in
case of overloading.
❮ Previous Next ❯
Comments
sirisha says
AUGUST 27, 2014 AT 3:56 PM
Reply
sweety says
OCTOBER 13, 2014 AT 6:38 PM
Reply
Reply
vishal says
NOVEMBER 13, 2014 AT 3:45 PM
very clear….
Reply
prerana says
NOVEMBER 13, 2014 AT 9:40 PM
The examples helped very much. The examples were clear, and to the point. Great
explanation. :D
Reply
Mayur says
JANUARY 4, 2015 AT 9:12 AM
Reply
Leesa says
JANUARY 28, 2015 AT 3:56 PM
The case 4-
oat mymethod(int a, oat b)
oat mymethod( oat var1, int var2)
causes a compilation error due to implicit method invocation datatype conversion.
Please check ..
Reply
Shipra says
JUNE 15, 2016 AT 11:27 AM
that’s right
Reply
hayenadeblue says
NOVEMBER 23, 2016 AT 12:30 PM
I think what is written in the article is correct. Don’t understand why Shipra agrees.
Reply
Reply
Noah says
MARCH 6, 2015 AT 7:52 PM
There couldn’t be a more detailed and useful explanation of method overloading than
this.
Thank you!
Reply
chetan says
MARCH 10, 2015 AT 5:41 AM
You have described in well manner i like that your presentation way………….Please post
more on same way………:)Thanks
Reply
Reply
Kalhan says
MARCH 16, 2015 AT 4:58 PM
Reply
Suresh says
MARCH 28, 2015 AT 5:43 PM
Nice Example
Reply
Arpitha says
APRIL 9, 2015 AT 6:38 AM
Good Explanation :)
Thank you
Reply
surekha says
JULY 15, 2015 AT 9:48 AM
Reply
sam says
JULY 16, 2015 AT 10:32 AM
HI,
In the below
Case 4:
Reply
Reply
Ganesh NB says
AUGUST 3, 2015 AT 11:24 AM
Great Explanation. Can u make it clear wheather java supports pass by reference.I know
its no. still the value are updated in the calling function. can you explain how it
occurs…?
Reply
Reply
Kemoji says
MARCH 1, 2016 AT 6:08 AM
Reply
Md Nehaluddin Haider says
MARCH 6, 2016 AT 5:16 AM
I tried reading many tutorials for OOPs and now I can say this is the best. Thank you so
much buddy for this wonderful tutorial.
Reply
Ankit says
APRIL 13, 2016 AT 9:57 AM
Thanks
Reply
Thank you!
Reply
akshay says
JUNE 8, 2016 AT 6:20 PM
Reply
Luke says
JUNE 18, 2016 AT 1:50 PM
Reply
Pankaj says
JUNE 20, 2016 AT 2:52 PM
when we call them with values, it will throw an error like error: reference to print is
ambiguous h.print(10, 20);
cause both are taking int value, that’s why reference variable is getting confused while
selecting/calling them,
Reply
Kd says
JULY 6, 2016 AT 7:29 PM
Reply
pooja says
SEPTEMBER 12, 2016 AT 3:21 PM
Reply
venkat says
OCTOBER 2, 2016 AT 10:01 PM
F:javaprograms>javac TestOverloading3.java
F:javaprograms>java TestOverloading3
Error: Could not nd or load main class TestOverloading3
its compiling successfully ,but its showing error like this while run.
may i know why i am getting like this error
Reply
Shiva says
NOVEMBER 13, 2016 AT 7:07 PM
Method Overloading Concepts are very clearly understood and got registered in my
mind. Thanks a lot.
Reply
throw an error.
Reply
Reply
Thank you so much.the notes are clear and easy to follow.please keep up doing the good
job, you have assisted me so much
Reply
Reply
Can we take input at compile time in case of method overloading •••••?With the help of
keyboard???
Reply
Leave a Reply
Your email address will not be published. Required elds are marked *
Comment
Name *
Email *
POST COMMENT
Java Tutorial
Java Index
Java Introduction
Variables
Data Types
Operators
Java Control
Statements
Java If-else
Java Switch-Case
Continue statement
break statement
OOPs Concepts
OOPs Concepts
Constructor
Static keyword
Inheritance
Types of inheritance
Aggregation
Association
Super Keyword
Method overloading
Method overriding
Overloading vs
Overriding
Polymorphism
Types of
polymorphism
Interface
Abstract class vs
interface
Encapsulation
Packages
Garbage Collection
Inner classes
Static import
Static constructor
Java Interview Q
MORE ...
Java 8 Features
Java 9 Features
Java Conversion
Java String
Exception handling
Java Multithreading
Java I/O
Java Serialization
Java Regex
Java AWT
Java Swing
Java Enum
Java Annotations