Method Overloading in Java
Method Overloading in Java
com
http://www.javatpoint.com/method-overloading-in-java
In java, Methood Overloading is not possible by changing the return type of the method.
3.
4.
5.
6.
obj.sum(10,10,10);
7.
obj.sum(20,20);
8.
9. }
Test it Now
Output:30
40
1/5
3.
4.
5.
6.
obj.sum(10.5,10.5);
7.
obj.sum(20,20);
8.
9. }
Test it Now
Output:21.0
40
Que) Why Method Overloaing is not possible by changing the return type of method?
In java, method overloading is not possible by changing the return type of the method because there may
occur ambiguity. Let's see how ambiguity may occur:
because there was problem:
1. class Calculation3{
2.
3.
4.
5.
6.
int result=obj.sum(20,20);
7.
8. }
Test it Now
int result=obj.sum(20,20); //Here how can java determine which sum() method should be called
3.
System.out.println(a);
4.
5.
6.
7.
main(10);
2/5
8.
9. }
Test it Now
Output:main() method invoked
10
As displayed in the above diagram, byte can be promoted to short, int, long, float or double. The short datatype
can be promoted to int,long,float or double. The char datatype can be promoted to int,long,float or double and
so on.
3.
4.
5.
6.
obj.sum(20,20);
7.
obj.sum(20,20,20);
8.
3/5
9. }
Test it Now
Output:40
60
3.
4.
5.
6.
obj.sum(20,20);
7.
8. }
Test it Now
Output:int arg method invoked
3.
4.
5.
6.
obj.sum(20,20);
7.
8. }
Test it Now
Output:Compile Time Error
One type is not de-promoted implicitly for example double cannot be depromoted to any
type implicitely.
Next TopicConstructor in java
< next>>
4/5
5/5