Method Overloading (function overloading) in Java

By: Abinaya Emailed: 1770 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are different. When this is the case, the methods are said to be overloaded, and the process is referred to as method overloading. Method overloading is one of the ways that Java implements polymorphism.

If you have never used a language that allows the overloading of methods, then the concept may seem strange at first. But as you will see, method overloading is one of Java's most exciting and useful features. When an overloaded method is invoked, Java uses the type and/or number of arguments as its guide to determine which version of the overloaded method to actually call. Thus,
overloaded methods must differ in the type and/or number of their parameters. While overloaded methods may have different return types, the return type alone is insufficient to distinguish two versions of a method. When Java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call. Here is a simple example that illustrates method overloading:

// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
// overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a;
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test(10, 20);
result = ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);

This program generates the following output:

No parameters
a: 10
a and b: 10 20
double a: 123.2
Result of ob.test(123.2): 15178.24

As you can see, test( ) is overloaded four times. The first version takes no parameters, the second takes one integer parameter, the third takes two integer parameters, and the fourth takes one double parameter. The fact that the fourth version of test( ) also returns a value is of no consequence relative to overloading, since return types do not play a role in overload resolution.

When an overloaded method is called, Java looks for a match between the arguments used to call the method and the method's parameters. However, this match need not always be exact. In some cases Java's automatic type conversions can play a role in overload resolution. For example, consider the following program:

// Automatic type conversions apply to overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
// overload test for a double parameter
void test(double a) {
System.out.println("Inside test(double) a: " + a);
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
int i = 88;
ob.test(10, 20);
ob.test(i); // this will invoke test(double)
ob.test(123.2); // this will invoke test(double)

This program generates the following output:

No parameters
a and b: 10 20
Inside test(double) a: 88
Inside test(double) a: 123.2

As you can see, this version of OverloadDemo does not define test(int). Therefore, when test( ) is called with an integer argument inside Overload, no matching method is found. However, Java can automatically convert an integer into a double, and this conversion can be used to resolve the call. Therefore, after test(int) is not found, Java elevates i to double and then calls test(double). Of course, if test(int) had been defined, it would have been called instead. Java will employ its automatic type conversions only if no exact match is found.

Method overloading supports polymorphism because it is one way that Java implements the "one interface, multiple methods" paradigm. To understand how, consider the following. In languages that do not support method overloading, each method must be given a unique name. However, frequently you will want to implement essentially the same method for different types of data. Consider the absolute value function. In languages that do not support overloading, there are usually three or more versions of this function, each with a slightly different name. For instance, in C, the function abs( )
returns the absolute value of an integer, labs( ) returns the absolute value of a long integer, and fabs() returns the absolute value of a floating-point value. Since C does not support overloading, each function has to have its own name, even though all three functions do essentially the same thing. This makes the situation more complex, conceptually, than it actually is. Although the underlying concept of each function is the same, you still have three names to remember. This situation does not occur in Java, because each absolute value method can use the same name. Indeed, Java's standard class library includes an absolute value method, called abs( ). This method is overloaded
by Java's Math class to handle all numeric types. Java determines which version of abs() to call based upon the type of argument.

The value of overloading is that it allows related methods to be accessed by use of a common name. Thus, the name abs represents the general action which is being performed. It is left to the compiler to choose the right specific version for a particular circumstance. You, the programmer, need only remember the general operation being performed. Through the application of polymorphism, several names have been reduced to one. Although this example is fairly simple, if you expand the concept, you can see how overloading can help you manage greater complexity.

When you overload a method, each version of that method can perform any activity you desire. There is no rule stating that overloaded methods must relate to one another. However, from a stylistic point of view, method overloading implies a relationship. Thus, while you can use the same name to overload unrelated methods, you should not. For example, you could use the name sqr to create methods that return the square of an integer and the square root of a floating-point value. But these two operations are fundamentally different. Applying method overloading in this manner defeats its original purpose. In practice, you should only overload closely related operations.

This is an extract from the book Java: the complete reference. By Herbert Schildt

Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

Great! Thanks!

View Tutorial          By: sciarp at 2008-05-11 05:04:17
2. View Comment

good job

View Tutorial          By: gopi at 2008-07-01 02:02:48
3. View Comment

plz explain in
simple way like give easiy examples but other wise thank for give me a prechies infor.

View Tutorial          By: mani at 2008-10-20 01:32:50
4. View Comment

Is operator overloading possible in java??????

View Tutorial          By: rishi at 2008-11-01 23:55:55
5. View Comment

thanks this material is very helpful for me

View Tutorial          By: Anonymous at 2009-05-09 08:04:31
6. View Comment

Great Thanks

View Tutorial          By: seedimani at 2009-09-16 01:27:26
7. View Comment

it very useful to under stand

View Tutorial          By: gurav gupta at 2009-09-24 00:20:15
8. View Comment

Thanks this page is very useful for me and now I know something I missed in my class about method overloading lessons.

View Tutorial          By: Yee Wai Wyne at 2009-10-18 23:03:41
9. View Comment

This example is very clear. I cleared my concept of overloading here. Thanks

View Tutorial          By: sukanta at 2009-10-20 06:08:50
10. View Comment

it is nice but please give simple example like area of rectangel

View Tutorial          By: red at 2009-12-03 23:07:37
11. View Comment

it's very good to understand...and it is helpful for learners...who not understand about overloading.....

View Tutorial          By: Rakesh at 2009-12-26 00:14:59
12. View Comment

Thank you! :) But I have a question, why is that the value of result in the first code is 15178.24? Sorry, but I'm still new in using Java... Answer is well-appreciated! Thank you!

View Tutorial          By: choco at 2010-01-03 02:16:18
13. View Comment

Sorry, please disregard my previous comment... Thank you for the good discussion! :) God bless! :)

View Tutorial          By: choco at 2010-01-03 02:21:09
14. View Comment

thank you..........

View Tutorial          By: ravi at 2010-01-29 23:27:39
15. View Comment

if return type is different but parameters same does overloading occurs??????????????????

View Tutorial          By: dodo at 2010-04-08 23:08:58
16. View Comment

@dodo : Overloading is only for parameter types and the return type wont influence the overloading..

View Tutorial          By: sundar at 2010-04-19 02:44:23
17. View Comment

very simple and nice examples for begineer...

View Tutorial          By: sarhand at 2010-04-21 00:57:23
18. View Comment

return type doesn't influence the overloading. Only parameter type and number of parameter need to be taken care.

View Tutorial          By: Shekhar at 2010-05-17 01:22:04
19. View Comment

thankssss to prograem

View Tutorial          By: maha at 2010-05-21 05:14:36
20. View Comment

very simple to understand

View Tutorial          By: chermaraja at 2010-05-26 00:00:05
21. View Comment

what about the access modifiers, while talking about the method overloading in java?

View Tutorial          By: Seetha at 2010-06-04 01:14:02
22. View Comment

very nice program but more info require

View Tutorial          By: pradeep at 2010-07-15 01:11:37
23. View Comment

Good job.

View Tutorial          By: Som2ie at 2010-08-02 05:07:37
24. View Comment

nice to read thank you for the notes. that helps me to learn java.......

View Tutorial          By: V.Ranjith kumar at 2010-08-12 06:13:03
25. View Comment

It's helpful to me.. to understand stand clear picture of method overloading concept... with good example.

View Tutorial          By: Ansari at 2010-08-18 10:22:54
26. View Comment

vry nice example....thanx..

View Tutorial          By: rekha at 2010-09-14 11:55:38
27. View Comment

gud example, we welcome you

View Tutorial          By: jasim Khalid almurakh meherbani at 2010-09-20 03:14:25
28. View Comment

not good not bad...............

View Tutorial          By: mahesh at 2010-10-05 00:45:54
29. View Comment

it is very understandable thanku very much......

View Tutorial          By: manogna at 2010-10-13 02:24:06
30. View Comment

I am very comfortable with this example, its very nice!

View Tutorial          By: Inam at 2010-11-07 03:04:46
31. View Comment

good job
by- shiv at 2010-11-26 11:51:20

View Tutorial          By: shiv kumar bind at 2010-11-25 23:20:28
32. View Comment

easy to understand

View Tutorial          By: shanthi at 2011-01-02 21:25:51
33. View Comment

method overloading possible across classes????

View Tutorial          By: Rohit at 2011-01-26 20:23:02
34. View Comment

thnkss buddyy its very helpful for my assignmentss.....

View Tutorial          By: yamini at 2011-02-25 21:01:23
35. View Comment

thank you :)
really helped... :)

View Tutorial          By: Tejas at 2011-03-22 14:17:46
36. View Comment

haii , i am fully happy by searching all components and clarifying my doubts ...

best regards,
thanks ,

View Tutorial          By: AJEET KUMAR DALEI at 2011-04-23 21:20:00
37. View Comment

That's really daam !!! simple example :) ...............

thanx ...................

View Tutorial          By: akshay sharma at 2011-05-03 04:49:15
38. View Comment

very nice approach sir....
u make it very easy for us.
thank u

View Tutorial          By: aamir at 2011-06-15 01:29:22
39. View Comment

Good Luck.........

by Nagarajan(AVRM).,

View Tutorial          By: nagarajan at 2011-06-21 12:14:39
40. View Comment

Thanks a lot!
The main reason why we need to overload a method by giving an example like this wasn't there in my study material, which is why I was confused. Not anymore, though! Thanks again!

View Tutorial          By: SA at 2011-06-24 15:00:13
41. View Comment

I request to java beacuse i hope java language
regartid by
jeey kamil

View Tutorial          By: jeey at 2011-07-06 07:59:07
42. View Comment

its easy to understand your example in overloading method...
And its helpful to us who were new in java programming and to those who were not good in programming class.

View Tutorial          By: rovie at 2011-09-07 02:41:12
43. View Comment

Very good examples are given by you on overloading. Can you please tell me how can i get some ease and effective example of the java program of INHERITANCE.
Thnx in advance.

View Tutorial          By: Ankur Rautela at 2011-11-03 10:22:46
44. View Comment

it's very good example to understands overloading

View Tutorial          By: Rakesh Sharma at 2011-11-18 06:37:49
45. View Comment

thats is really too gud... very easy example to understand...

View Tutorial          By: radhika at 2011-11-27 16:32:25
46. View Comment

thank you soo much .. itss realy helpful.... :)

View Tutorial          By: prakriti at 2011-12-03 16:47:11
47. View Comment

thanks for giving suitable example to understand method overloding in java.

View Tutorial          By: akash dange at 2011-12-21 05:40:36
48. View Comment

It is very Important Site

View Tutorial          By: Shridhar Koli at 2011-12-29 11:43:29
49. View Comment

it was very useful to me

View Tutorial          By: balaji at 2012-01-09 22:50:25
50. View Comment

I am new in Java and got good information to understanding overloading.


View Tutorial          By: Umesh Kumar at 2012-01-25 08:42:07
51. View Comment

nice explainations for the beginners like me :)

View Tutorial          By: Divya Taneja at 2012-02-11 13:17:17
52. View Comment

Good one. Keep up the good work.

View Tutorial          By: zubair at 2012-02-23 10:18:36
53. View Comment

it is very nice example of overloading when i read this example my concept clear better then before tanks..... a lots

View Tutorial          By: narendra saini at 2012-04-20 04:03:56
54. View Comment

I have a small query: Is it interface allow method overloading type declaration of method?

Ex: public interface SampleInterface {

public void display(String name);
public void display(String name,int age);

Is it correct interface defining or not?

Thanks in Advance.

View Tutorial          By: vijay at 2012-04-20 10:34:30
55. View Comment

very informative

View Tutorial          By: santhosh reddy WILLY at 2012-07-06 21:33:39
56. View Comment

Its informative and helpful. Thanx

View Tutorial          By: Usama at 2012-07-14 22:16:45
57. View Comment


View Tutorial          By: chalamareddy at 2012-10-08 18:08:29
58. View Comment

thanks,Gud job

View Tutorial          By: junaid sheikh at 2012-11-21 16:24:27
59. View Comment

Very well explained Thanks.

View Tutorial          By: Anthony at 2012-12-12 00:20:39
60. View Comment

thanks a lot............

View Tutorial          By: mks at 2013-01-03 06:50:05
61. View Comment

thanks...........its really very helpful for me........

View Tutorial          By: ashwini agarmore at 2013-01-17 06:31:21
62. View Comment

very nice example

View Tutorial          By: harshal at 2013-02-06 09:34:10
63. View Comment

very nice notes we are easy to understand this notes thank u.....!

View Tutorial          By: maalu at 2013-04-29 07:06:56
64. View Comment

can help me for program in java Tomorrow I have exam in java?????????

View Tutorial          By: blasm at 2013-06-21 06:59:41
65. View Comment

hey pls tel me, what is the advantage of method overloding?

View Tutorial          By: divya at 2013-08-27 14:20:22
66. View Comment

Its very clearly explained.

View Tutorial          By: sowjanya at 2013-10-04 09:26:04
67. View Comment

wow........thanks a lot :)

View Tutorial          By: shiva at 2015-08-11 07:47:08
68. View Comment

whether the below command will work
Math operation m = new methodoverloading();

View Tutorial          By: Gowrish Sagar at 2015-10-05 10:36:35

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Abinaya
How to compile a Java program - javac
The clone() Method in Java
Data Types in Java
JSP Example to connect to MS SQL database and retrieve records
faces-config.xml to DirectTraffic in the JSF Application
Enabling Expression Language Evaluation in JSP
Using malloc() Function in C
ActionErrors and ActionError in Struts
Open, Creat, Close, Unlink system calls sample program in C
Structures and Functions in C
Introduction to JSP expression language
Multi-dimensional Arrays in C (Explained using date conversion program)
A C program similar to grep command in UNIX
Type Conversions in C (String to Integer, isdigit() etc)
Basics of C

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
Student marks calculation program in Java
FileReader and FileWriter example program in Java
Execute system commands in a Java Program
Why java is important to the Internet
Count number of vowels, consonants and digits in a String in Java
Calculate gross salary in Java
Vector example in Java
How to use ArrayList in Java
Read from a COM port using Java program
Tomcat and httpd configured in port 8080 and 80
Palindrome String in Java
The Unary Operators example in Java
right shift operator, >>, in Java
Integer: byte, short, int, and long data types in Java
XML and Java - Parsing XML using Java Tutorial
Most Emailed Articles (in Java)
Using Charts in JasperReports
Introducing Nested and Inner Classes
Tomcat and httpd configured in port 8080 and 80
List all the serial and parallel ports in your system using Java
What is Unicode?
FileReader and FileWriter example program in Java
FilenameFilter - sample program in Java
Basics of Inheritance in Java
Method Overloading (function overloading) in Java
while - Iteration in java
how to use boolean data type in Java
equals( ) Versus == in Java
J2EE Components
Increment and Decrement Operator
Read from a COM port using Java program