Sunday, 24 April 2016

is Method overloading possible by changing the return type of method ? Why ?


No
, as it leads to ambiguity :-S

Example
class Test { 
  int sum(int a, int b) {
     int x = a+b;
  } 
  
  double sum(int a, int b){
     double x = a+b;
  } 

  public static void main(String args[]){ 
    Test test = new Test(); 
    int result = test.sum(20,20);   // Compile time Error
               // Ambiguity : which method to call ?
               // As both have same type and sequence of argument 
  } 
}  


No comments:

Post a Comment

Note: only a member of this blog may post a comment.