Method Overloading (Cont.)
Advantages of Method Overloading
We don’t have to create and remember different names for functions doing the same thing.
For example, if overloading was not supported, method names like sum1
, sum2Int
, etc. have to be created in the previous code.
Different Ways to Overload the Method
There are two ways to overload the method:
- by changing number of arguments, and
- by changing the data type.
We cannot overload by return type.
This behavior is same in C++.
However, overloading methods on return type are possible in cases where the data type of the function being called is explicitly specified.
For the following example, the command
foo('x')
will call the method
foo
with a
char
input parameter:
Birds of a feather flock together.
|