Home > DeveloperSection > Interviews > Why method overloading is not possible by changing the return type in java?

Posted on    March-30-2015 2:14 AM

 Java Java 
Ratings:
 1 Answer(s)
  846  View(s)
Rate this:

Mayank Tripathi
Mayank Tripathi

Total Post:807

Points:4035
Posted on    March-30-2015 2:42 AM

n java, method overloading is not possible by changing the return type of the method because there may occur ambiguity.here is an example which state how ambiguity might occur:

here how java determine which method to call 

class Calculation3{  
  int sum(int a,int b){System.out.println(a+b);}  
  double sum(int a,int b){System.out.println(a+b);}  
  
  public static void main(String args[]){  
  Calculation3 obj=new Calculation3();  
  int result=obj.sum(20,20); //Compile Time Error  
  
  }  
}   

Modified On Mar-30-2015 02:43:53 AM

Don't want to miss updates? Please click the below button!

Follow MindStick