• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AmbiguosMethodCall

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//Question :
//What happens when the following class is compiled & run ?

class AmbiguosMethodCall {

void something ( int i ) {
System . out . println ( i ) ;
}

int something ( int i ) {
System . out . println ( i + 1 ) ;
return i + 1 ;
}

public static void main ( String args [ ] ) {
AmbiguosMethodCall obj = new AmbiguosMethodCall ( ) ;
obj . something ( 67 ) ;
}
}

/*
Options :

a . Compiles fine but throws AmbigousMethodInvocationException at runtime
b . Compiler error - the call to method something ( ) is ambiguos
c . Compiler error - methods can't be defined with a different return type
d . Compiles fine & prints 67 at runtime
*
*How come is the answer b. How come the compiler
*treat void somethig(int) and int something(int) as same
*same methods???. I think they are netiher overloaded
*nor overridden. They are just two differnt methods with
*same names/
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overloading rule - " Must change argument list, but changing return type is optional. "
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is nothing to with the call of an ambiguous method. There is a duplicate method definition. Two methods with the same signature, with different return types, in the same class. That's the real error.

The compiler error in main, is just because the method something is not properly defined at that point (because of the previous error).

So, in my opinion, c is the correct answer.
[ December 05, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 354
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler error should be - "method is already defined" instead of "the call is ambiguous " (in jdk1.4 at least)
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer should be b.
c is not right because methods can have different return types if argument list is also different.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But here, the argument list is the same (int i).

Error should be something like "duplicate method something(int)..."
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am for the "C" option.
The 2 methods will cause a complier error.
Simply changing the return type,they are neither overidding nor overloading.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AmbiguosMethodCall.java : somethin(int) is already defined.

compiler error is obtained .



given options c is not correct bcoz it says method with different return types are not allowed.
which is wrong if the methods are overloaded.
now we should not think that these options refer to this question only.


option b is correct because as the error states the something(int) is already defined. so it is ambiguos to call this method.
reply
    Bookmark Topic Watch Topic
  • New Topic