Method Overloading (function overloading) in Java

By: Abinaya Viewed: 228332 times  Printer Friendly Format    


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



Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

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

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

3. plz explain in
simple way like give ea

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

4. Great post. <a href="http://javarevisited.
View Tutorial          By: Java Blog at 2012-10-03 03:11:44

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

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

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

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

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

10. Thanks this page is very useful for me and now I k
View Tutorial          By: Yee Wai Wyne at 2009-10-18 23:03:41

11. This example is very clear. I cleared my concept o
View Tutorial          By: sukanta at 2009-10-20 06:08:50

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

13. it's very good to understand...and it is helpful f
View Tutorial          By: Rakesh at 2009-12-26 00:14:59

14. Thank you! :) But I have a question, why is that t
View Tutorial          By: choco at 2010-01-03 02:16:18

15. Sorry, please disregard my previous comment... Tha
View Tutorial          By: choco at 2010-01-03 02:21:09

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

17. if return type is different but parameters same do
View Tutorial          By: dodo at 2010-04-08 23:08:58

18. @dodo : Overloading is only for parameter types an
View Tutorial          By: sundar at 2010-04-19 02:44:23

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

20. return type doesn't influence the overloading. Onl
View Tutorial          By: Shekhar at 2010-05-17 01:22:04

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

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

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

24. what about the access modifiers, while talking abo
View Tutorial          By: Seetha at 2010-06-04 01:14:02

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

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

27. nice to read thank you for the notes. that helps m
View Tutorial          By: V.Ranjith kumar at 2010-08-12 06:13:03

28. It's helpful to me.. to understand stand clear pic
View Tutorial          By: Ansari at 2010-08-18 10:22:54

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

30. you are doing a great job
KEEP IT UP

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

48. thank you :)
really helped... :)

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

49. haii , i am fully happy by searching all component
View Tutorial          By: AJEET KUMAR DALEI at 2011-04-23 21:20:00

50. haii , i am fully happy by searching all component
View Tutorial          By: AJEET KUMAR DALEI at 2011-04-23 21:20:03

51. That's really daam !!! simple example :) .........
View Tutorial          By: akshay sharma at 2011-05-03 04:49:15

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

53. very nice approach sir....
u make it very e

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

54. very nice approach sir....
u make it very e

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

55. Good Luck.........

by Nagarajan(

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

56. 100% copied from Herbert Schildt......i read Compl
View Tutorial          By: Sajal at 2011-06-24 00:55:14

57. Thanks a lot!
The main reason why we need t

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

58. I request to java beacuse i hope java language

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

59. this is best example to explain method overloading
View Tutorial          By: ashish bhardwaj at 2011-08-03 13:16:18

60. thank u sir,this example for overloding is best an
View Tutorial          By: deepak sharma at 2011-08-03 19:01:33

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

62. its easy to understand your example in overloading
View Tutorial          By: rovie at 2011-09-07 02:41:12

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

64. thank you sir :
very nice examples:

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

65. Wonderful stuff. It can't be put any simpler. The
View Tutorial          By: Mrammaji at 2011-09-26 08:11:47

66. Very good examples are given by you on overloading
View Tutorial          By: Ankur Rautela at 2011-11-03 10:22:46

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

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

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

70. thanks for giving suitable example to understand m
View Tutorial          By: akash dange at 2011-12-21 05:40:36

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

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

73. I am new in Java and got good information to under
View Tutorial          By: Watch Movies Online at 2012-01-25 08:40:52

74. I am new in Java and got good information to under
View Tutorial          By: Umesh Kumar at 2012-01-25 08:42:07

75. nice explainations for the beginners like me :)
View Tutorial          By: Divya Taneja at 2012-02-11 13:17:17

76. Program about <a href="http://extreme-java
View Tutorial          By: Extreme Java at 2012-02-18 09:01:21

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

78. I already know abt Method Overloading .. U have ex
View Tutorial          By: tamee at 2012-02-21 12:32:31

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

80. it is very nice example of overloading when i read
View Tutorial          By: narendra saini at 2012-04-20 04:03:56

81. I have a small query: Is it interface allow method
View Tutorial          By: vijay at 2012-04-20 10:34:30

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

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

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

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

86. itz very easy 2 understand...
thankx a lot

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

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

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

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

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

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

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

93. very nice notes we are easy to understand this not
View Tutorial          By: maalu at 2013-04-29 07:06:56

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

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

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

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

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

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

100. If you are interested in Method overriding in Java
View Tutorial          By: maddy at 2014-01-09 09:39:18

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

102. whether the below command will work
Math o

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

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

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

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

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

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

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

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

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

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

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

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

114. Hi. Is it possible to do method overloading withou
View Tutorial          By: Tushar at 2016-03-22 11:15:41

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

116. Just look at this wouldn't be fine, whether the be
View Tutorial          By: happy new year 2017 at 2016-05-14 15:29:24

117. Its very informative and descriptive. Better way t
View Tutorial          By: Aanchal at 2016-07-31 08:26:40

118. Hum Aapke Dill Main Rahte Hain!
Saare Dard

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

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

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

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

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

123. whether the below command will work
Math o

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

124. Happy new Year 2017 we have included New YearImag
View Tutorial          By: Deviel Back at 2016-12-18 17:57:04

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

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

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

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

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

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

131. Hi just wanted to give you a quick heads up and le
View Tutorial          By: astrology at 2017-04-02 04:41:44