Method overloading can be achieved by defining multiple methods in the same class with the method name being same but the parameters are different.
In the below class
ArithmeticUtils we have defined 5 methods out of which three methods have the name add and two methods have the name multiply . As discussed in Java Methods, there is no problem in having the same method name for one or more methods as long as the method signature is different. The method signature includes the return type, method name and the parameters. As long as the parameters are different (the type, number and the order of the parameters) it is allowed to create as many methods with the same method name.
class ArithmeticUtils add method is overloaded once with two int parameters (LINE A ), once with three int parameters (LINE B ) and once with three double parameters (LINE C ). We can define more methods with the same name add as long as the parameters are different. The following methods can be defined.
double add(int a, double b) int add(int a, int b) int add(int a, int b)
|