Introduction to Method Overloading
Method overloading is one of the important features in any programming language which is used for names. When you create an object, you give a name to a region of storage. A method is a name for an action. By using names to describe your system, you create a program that is easier for people to understand and change. It’s a lot like writing prose—the goal is to communicate with your readers.
You refer to all objects and methods by using names. Well-chosen names make it easier for you and others to understand your code.
A problem arises when mapping the concept of nuance in human language onto a programming language. Often, the same word expresses a number of different meanings—it’s overloaded. This is useful, especially when it comes to trivial differences. You say “wash the shirt,” “wash the car,” and “wash the dog.” It would be silly to be forced to say, “shirtWash the shirt,” “carWash the car,” and “dogWash the dog” just so the listener doesn’t need to make any distinction about the action performed. Most human languages are redundant, so even if you miss a few words, you can still determine the meaning. We don’t need unique identifiers—we can deduce meaning from context.
Most programming languages (C in particular) require you to have a unique identifier for each function. So you could not have one function called print( ) for printing integers and another called print( ) for printing floats—each function requires a unique name.
In Java (and C++), another factor forces the overloading of method names: the constructor. Because the constructor’s name is predetermined by the name of the class, there can be only one constructor name. But what if you want to create an object in more than one way? For example, suppose you build a class that can initialize itself in a standard way or by reading information from a file. You need two constructors, one that takes no arguments (the default constructor, also called the no-arg constructor), and one that takes a String as an argument, which is the name of the file from which to initialize the object. Both are constructors, so they must have the same name—the name of the class. Thus, method overloading is essential to allow the same method name to be used with different argument types. And although method overloading is a must for constructors, it’s a general convenience and can be used with any method.
Method overloading in Java works only when method names are same and their parameter lists are different—either in number or type of arguments.
Return type is not part of a method signature, so it’s not considered when methods have been overloaded.
Syntax
class MyClass {
//fields and constructors
Public void show (int i) {
System.out.println(“Integer: ” + i);
}
Public void show (String str) {
System.out.println(“String: ” + str);
}
}
Example
1. /**
2. * @(#)OverloadingDemo.java
3. *
4. *
5. * @author
6. * @version 1.00 2009/11/18
7. */
8.
9. class Message {
10.
11. String msg;
12.
13. public Message() {
14. msg = "Sorry! There is no message.";
15. }
16.
17. public Message(String str) {
18.
19. msg = str;
20. }
21.
22. public void show() {
23.
24. System.out.println(msg);
25.
26. }
27.
28. public void show(int i) {
29.
30. System.out.println("Integer value: " + i);
31.
32. }
33.
34. public void show(float f) {
35.
36. System.out.println("Decimal value: " + f);
37.
38. }
39.
40. public void show (String str) {
41.
42. System.out.println("String value: " + str);
43.
44. }
45.
46.
47. }
48.
49. public class OverloadingDemo {
50.
51. /**
52. * Creates a new instance of OverloadingDemo.
53. */
54. public OverloadingDemo() {
55. }
56.
57. /**
58. * @param args the command line arguments
59. */
60. public static void main(String[] args) {
61. // TODO code application logic here
62.
63. //Create object with default constructor
64. Message msgObj = new Message();
65.
66. //Print default message
67. msgObj.show();
68.
69. //Create object with overloaded constructor
70. Message msgObj2 = new Message("Hello World!");
71.
72. //Show method is overloaded
73. msgObj2.show();
74. msgObj2.show(123);
75. msgObj2.show((float)45.54);
76. msgObj2.show("Test");
77.
78.
79. }
80. }
Output
Sorry! There is no message.
Hello World!
Integer value: 123
Decimal value: 45.54
String value: Test
