Method Overloading (function overloading) in Java

By: Abinaya  

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();
ob.test(10);
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();
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




Archived Comments

1. Hi just wanted to give you a quick heads up and let you know a few of the pictures aren't loading pr
View Tutorial          By: astrology at 2017-04-02 04:41:44

2. oicuben
View Tutorial          By: oicuben at 2017-03-19 05:22:47

3. ukaquapewek
View Tutorial          By: ukaquapewek at 2017-03-18 07:58:33

4. Guestchoob
View Tutorial          By: Guestchoob at 2017-02-23 09:49:18

5. nicely explained.
View Tutorial          By: sam at 2017-01-15 01:01:06

6. awesome post thanks for share ..
View Tutorial          By: mr-jatt at 2017-01-05 00:44:54

7. This tutorial is great
View Tutorial          By: dhruvhat at 2016-12-25 11:39:51

8. Happy new Year 2017 we have included New YearImages, New Year Greetings, New Year SMS.
View Tutorial          By: Deviel Back at 2016-12-18 17:57:04

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

View Tutorial          By: internet balance check codes at 2016-12-07 12:23:52

10. An Excellent Post thanks for sharing you also May like this.
View Tutorial          By: Barkha Kapoor at 2016-11-23 13:34:25

11. inforrmation is very helpful.
View Tutorial          By: Happy New Year Wallpaper at 2016-10-05 08:26:32

12. http://www.happynewyearwishesquotes.com
View Tutorial          By: Happy New Year 2017 Quotes at 2016-10-04 06:34:17

13. very nice upon coding
View Tutorial          By: Pankaj Sehoriya at 2016-09-30 09:15:16

14. Hum Aapke Dill Main Rahte Hain!
Saare Dard Aap Ke Sahte Hai!
Koi Hum Se Pahle â&

View Tutorial          By: Happy New Year 2017 SMS at 2016-09-23 11:49:56

15. Its very informative and descriptive. Better way to understand method overloading.
View Tutorial          By: Aanchal at 2016-07-31 08:26:40

16. Just look at this wouldn't be fine, whether the below command will work
Math operation m = n

View Tutorial          By: happy new year 2017 at 2016-05-14 15:29:24

17. nice one!!!thanks alot
View Tutorial          By: mythri at 2016-04-05 11:55:43

18. Hi. Is it possible to do method overloading without passing the parameters in the method but defing
View Tutorial          By: Tushar at 2016-03-22 11:15:41

19. Very good article post.Much thanks again. Fantastic
View Tutorial          By: Happy Holi Pictures 2016 at 2016-02-12 09:55:55

20. Great, thanks for sharing this article.Thanks Again. Much obliged.
View Tutorial          By: CBSE 12th Result at 2016-02-11 15:33:57

21. Thanks for the blog post.Thanks Again. Will read on…
View Tutorial          By: Latest Rose Day SMS at 2016-02-02 15:06:43

22. Thanks, Very Informative one!
View Tutorial          By: pakyahdiez at 2015-12-22 12:48:52

23. wow........thanks a lot
View Tutorial          By: HD Wallpaper at 2015-12-21 14:11:11

24. wow........thanks a lot
View Tutorial          By: HD Wallpaper at 2015-12-21 14:10:26

25. Thanks A lot!!!!!!!!!!!!!!
View Tutorial          By: Amit at 2015-12-21 13:48:55

26. Nice Example!!! Great Work!
View Tutorial          By: Happy New Year at 2015-12-11 20:40:22

27. great post it's really helpful for me thnaks
View Tutorial          By: jaspreet at 2015-12-11 15:04:03

28. Wo Love you
View Tutorial          By: Merry Christmas 2015 Images at 2015-12-11 10:36:36

29. Thank you ever so for you blog article.Thanks Again. Really Great.
View Tutorial          By: Prem ratan dhan payo movie torrent at 2015-11-11 05:47:29

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

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

31. wow........thanks a lot :)
View Tutorial          By: shiva at 2015-08-11 07:47:08

32. If you are interested in Method overriding in Java please read more on this blog : Rules of Method O
View Tutorial          By: maddy at 2014-01-09 09:39:18

33. niece explanation thanks your new happy new year 2014 friend :D
View Tutorial          By: Ajeet New Year sms at 2013-12-23 15:33:17

34. Its very clearly explained.
View Tutorial          By: sowjanya at 2013-10-04 09:26:04

35. hey pls tel me, what is the advantage of method overloding?
View Tutorial          By: divya at 2013-08-27 14:20:22

36. velangala
View Tutorial          By: evano oruvan at 2013-06-21 12:17:30

37. velangala
View Tutorial          By: evano oruvan at 2013-06-21 12:14:25

38. can help me for program in java Tomorrow I have exam in java?????????
View Tutorial          By: blasm at 2013-06-21 06:59:41

39. very nice notes we are easy to understand this notes thank u.....!
View Tutorial          By: maalu at 2013-04-29 07:06:56

40. very nice example
View Tutorial          By: harshal at 2013-02-06 09:34:10

41. thanks...........its really very helpful for me........
View Tutorial          By: ashwini agarmore at 2013-01-17 06:31:21

42. thanks a lot............
View Tutorial          By: mks at 2013-01-03 06:50:05

43. Very well explained Thanks.
View Tutorial          By: Anthony at 2012-12-12 00:20:39

44. thanks,Gud job
View Tutorial          By: junaid sheikh at 2012-11-21 16:24:27

45. super
View Tutorial          By: chalamareddy at 2012-10-08 18:08:29

46. Great post. <a href="http://javarevisited.blogspot.tw/2011/08/what-is-polymorphism-in-java-e
View Tutorial          By: Java Blog at 2012-10-03 03:11:44

47. itz very easy 2 understand...
thankx a lot :)

View Tutorial          By: kala at 2012-08-12 09:57:19

48. Its informative and helpful. Thanx
View Tutorial          By: Usama at 2012-07-14 22:16:45

49. very informative
View Tutorial          By: santhosh reddy WILLY at 2012-07-06 21:33:39

50. thanks a lot. simple examples clarified my huge doubts. thanks again...
View Tutorial          By: Sivapriya chandru at 2012-06-25 10:50:20

51. That is Best for initial step of java programmer.
View Tutorial          By: Ankitsharma at 2012-06-21 08:24:08

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

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

53. it is very nice example of overloading when i read this example my concept clear better then before
View Tutorial          By: narendra saini at 2012-04-20 04:03:56

54. Good one. Keep up the good work.
View Tutorial          By: zubair at 2012-02-23 10:18:36

55. I already know abt Method Overloading .. U have explained it good ,. Nice work .
View Tutorial          By: tamee at 2012-02-21 12:32:31

56. ya its very useful for me... this is very helpful java begginners
View Tutorial          By: aravindan at 2012-02-18 20:04:30

57. Program about <a href="http://extreme-java.com/method-overloading-function-overloading-in-ja
View Tutorial          By: Extreme Java at 2012-02-18 09:01:21

58. nice explainations for the beginners like me :)
Thanks

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

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

Thanks

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

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

Thanks

View Tutorial          By: Watch Movies Online at 2012-01-25 08:40:52

61. it was very useful to me
View Tutorial          By: balaji at 2012-01-09 22:50:25

62. It is very Important Site
View Tutorial          By: Shridhar Koli at 2011-12-29 11:43:29

63. thanks for giving suitable example to understand method overloding in java.
View Tutorial          By: akash dange at 2011-12-21 05:40:36

64. thank you soo much .. itss realy helpful.... :)
View Tutorial          By: prakriti at 2011-12-03 16:47:11

65. thats is really too gud... very easy example to understand...
View Tutorial          By: radhika at 2011-11-27 16:32:25

66. it's very good example to understands overloading
View Tutorial          By: Rakesh Sharma at 2011-11-18 06:37:49

67. Very good examples are given by you on overloading. Can you please tell me how can i get some ease a
View Tutorial          By: Ankur Rautela at 2011-11-03 10:22:46

68. Wonderful stuff. It can't be put any simpler. The awesome thing is that 3 years after you posted it
View Tutorial          By: Mrammaji at 2011-09-26 08:11:47

69. thank you sir :
very nice examples:
really helped.....
by praveen patel at 2011

View Tutorial          By: praveen patel at 2011-09-25 09:52:07

70. very helpful.. simple and understandable manner.. thanks..
View Tutorial          By: Pradeep at 2011-09-20 17:22:12

71. its easy to understand your example in overloading method...
And its helpful to us who were n

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

72. how use the net bin plz explain
View Tutorial          By: lokendra singh at 2011-08-19 04:44:56

73. thank u sir,this example for overloding is best and easy in my according.
View Tutorial          By: deepak sharma at 2011-08-03 19:01:33

74. this is best example to explain method overloading..nice work man !!
View Tutorial          By: ashish bhardwaj at 2011-08-03 13:16:18

75. I request to java beacuse i hope java language
thanks
regartid by
jeey kami

View Tutorial          By: jeey at 2011-07-06 07:59:07

76. Thanks a lot!
The main reason why we need to overload a method by giving an example like this

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

77. 100% copied from Herbert Schildt......i read Complete Reference and came to net to clear my doubts..
View Tutorial          By: Sajal at 2011-06-24 00:55:14

78. Good Luck.........

by Nagarajan(AVRM).,

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

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

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

80. very nice approach sir....
u make it very easier for me...
thank u....

View Tutorial          By: AAMIR at 2011-06-15 01:12:58

81. stolen by herbert's book! huh!
View Tutorial          By: Shadab at 2011-05-06 14:26:05

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

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

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

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

best

View Tutorial          By: AJEET KUMAR DALEI at 2011-04-23 21:20:03

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

best

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

85. thank you :)
really helped... :)

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

86. thnkss buddyy its very helpful for my assignmentss.....
View Tutorial          By: yamini at 2011-02-25 21:01:23

87. method overloading possible across classes????
View Tutorial          By: Rohit at 2011-01-26 20:23:02

88. very clear and easiest examples
View Tutorial          By: Arjun at 2011-01-11 02:48:19

89. easy to understand
View Tutorial          By: shanthi at 2011-01-02 21:25:51

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

View Tutorial          By: shiv kumar bind at 2010-11-25 23:23:08

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

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

92. I am very comfortable with this example, its very nice!
View Tutorial          By: Inam at 2010-11-07 03:04:46

93. Very easy to understand good job....
View Tutorial          By: Umar at 2010-11-04 04:22:13

94. good example...thanx for programmer...
View Tutorial          By: saran at 2010-10-25 22:46:51

95. it is very understandable thanku very much......
View Tutorial          By: manogna at 2010-10-13 02:24:06

96. not good not bad...............
View Tutorial          By: mahesh at 2010-10-05 00:46:11

97. not good not bad...............
View Tutorial          By: mahesh at 2010-10-05 00:45:54

98. great.... thanks lot
View Tutorial          By: Rachana at 2010-10-03 06:10:32

99. gud example, we welcome you
View Tutorial          By: jasim Khalid almurakh meherbani at 2010-09-20 03:14:25

100. Thanks. Nice discussion.
View Tutorial          By: Anup at 2010-09-16 12:11:25

101. vry nice example....thanx..
View Tutorial          By: rekha at 2010-09-14 11:55:38

102. what are the disadvantage of using the function overloading
View Tutorial          By: rajesh at 2010-09-12 03:55:25

103. you are doing a great job
KEEP IT UP

View Tutorial          By: Deepak at 2010-09-10 10:58:22

104. thanks
View Tutorial          By: sam at 2010-08-28 01:50:51

105. It's helpful to me.. to understand stand clear picture of method overloading concept... with good ex
View Tutorial          By: Ansari at 2010-08-18 10:22:54

106. 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

107. Good job.
View Tutorial          By: Som2ie at 2010-08-02 05:07:37

108. very nice program but more info require
View Tutorial          By: pradeep at 2010-07-15 01:11:37

109. what about the access modifiers, while talking about the method overloading in java?
View Tutorial          By: Seetha at 2010-06-04 01:14:02

110. very simple to understand
View Tutorial          By: chermaraja at 2010-05-26 00:00:38

111. very simple to understand
View Tutorial          By: chermaraja at 2010-05-26 00:00:05

112. thankssss to prograem
View Tutorial          By: maha at 2010-05-21 05:14:36

113. return type doesn't influence the overloading. Only parameter type and number of parameter need to b
View Tutorial          By: Shekhar at 2010-05-17 01:22:04

114. very simple and nice examples for begineer...
View Tutorial          By: sarhand at 2010-04-21 00:57:23

115. @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

116. if return type is different but parameters same does overloading occurs??????????????????
View Tutorial          By: dodo at 2010-04-08 23:08:58

117. thank you..........
View Tutorial          By: ravi at 2010-01-29 23:27:39

118. 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

119. Thank you! :) But I have a question, why is that the value of result in the first code is 15178.24?
View Tutorial          By: choco at 2010-01-03 02:16:18

120. it's very good to understand...and it is helpful for learners...who not understand about overloadin
View Tutorial          By: Rakesh at 2009-12-26 00:14:59

121. it is nice but please give simple example like area of rectangel
View Tutorial          By: red at 2009-12-03 23:07:37

122. This example is very clear. I cleared my concept of overloading here. Thanks
View Tutorial          By: sukanta at 2009-10-20 06:08:50

123. Thanks this page is very useful for me and now I know something I missed in my class about method ov
View Tutorial          By: Yee Wai Wyne at 2009-10-18 23:03:41

124. thaanks alot
View Tutorial          By: vinaykumar at 2009-09-24 04:10:06

125. it very useful to under stand
View Tutorial          By: gurav gupta at 2009-09-24 00:20:15

126. Great Thanks
View Tutorial          By: seedimani at 2009-09-16 01:27:26

127. thanks this material is very helpful for me
View Tutorial          By: Anonymous at 2009-05-09 08:04:31

128. Is operator overloading possible in java??????
View Tutorial          By: rishi at 2008-11-01 23:55:55

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

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

130. good job
View Tutorial          By: gopi at 2008-07-01 02:02:48

131. Great! Thanks!
View Tutorial          By: sciarp at 2008-05-11 05:04:17


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial