To be a good programmer, understanding of Object oriented programming concepts is by far the most important thing. The one problem with freshmen programmers is that they are only being able to write classes, doing encapsulation and inheritance when asked about OOPs. It is not what you should call good OOP. So as an awareness and a guide this article talks about Method Overloading one of the OOP concept.
Method overloading is a feature found in various programming languages such as Ada, C#, C++, D and Java that allows the creation of several methods with the same name which differ from each other in terms of the type of the input and the type of the output of the function.
This article is Java specific and method overloading is one of the ways that Java implements polymorphism.
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.
// Demonstrate method overloading.
class OverloadExample {
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 a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadExample ex = new OverloadExample();
double fop;
// call all versions of test()
ex.test();
ex.test(50);
fop = ex.test(777.7);
System.out.println("a * a = " + fop);
}
Output :
No parameters
a: 50
double a: 777.7
a * a = 604817.29
When an overloaded method is called, the compiler looks for a match between the arguments used to call the method and the method’s parameters. This may not be right all the time. Take the below shown code for instance.
class OverloadExample {
// Overload test for one integer parameter.
void test(int a, int b) {
System.out.println("a: " + a +" b: " + b );
}
// overload test for a double parameter
void test(double a) {
System.out.println("(double) a: " + a);
}
}
class Overload {
public static void main(String args[]) {
OverloadExample ex = new OverloadExample();
double fop;
// call all versions of test()
ex.test (50, 60);
ex.test(50);
ex.test(777.7);
}
}
Which gives the output
a: 50 b: 60
(double) a: 50.0
(double) a: 777.7
This happens because, Java can automatically convert an integer into a double and thus results in the call test(double) for an integer value. 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.