• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is a most-specific method?  RSS feed

 
Mahi Mohana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I found this faq on http://faq.javaranch.com/java/ScjpFaq#mostSpecific

In this topic, there is statement saying--> If it's not possible to identify a most-specific method from among the applicable methods, then the invocation is considered "ambiguous" and results in a compile-time error.

Can anyone explain this with example, I mean in the above topic, by passing what argument can make that [b]method(whatevr) [/b] ambiguous to the compiler.

I always confuse when there is a overloaded primitive of same type and Wrapper of same type and var-args. Also, can anyone give few rules I need to check before answering that kind of question which can result in compilation error or no compilation error.

Thanks and Regards,
Hima
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This forum is for the discussion of certifcation results -- moving this topic to Java in General, Beginner.

Henry
 
Mahi Mohana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry,

Is there any forum for SCJP faqs. I mean where people preparing for SCJP can discuss the questions. If so Can you move this topic to there? Because I expect faster reply from them as they must be in the same wave length.

I am new here and I will take care to post the topics in the related forums.

Thanks
Hima
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this topic, there is statement saying--> If it's not possible to identify a most-specific method from among the applicable methods, then the invocation is considered "ambiguous" and results in a compile-time error.

Can anyone explain this with example, I mean in the above topic, by passing what argument can make that method(whatevr) ambiguous to the compiler.



In the FAQ that you quoted, there are two overloaded methods....

void method(Animal animal) {...}
void method(Cat cat) {...}


Now, if you call the method with .... method(null) ... it will call the version of the method that takes a Cat. As Cat is more specific than Animal -- Cat subclasses Animal.

But what if you had a third method?

void method(Animal animal) {...}
void method(Cat cat) {...}
void method(Dog dog) {...}


Now if you call the method with .... method(null) ... which method will it call? Since the parameter is null, it can't determine the type from it. And since both Dog and Cat subclasses from Animal, neither is more specific. Hence, you will get an ambiguous error here.

Henry
 
Mahi Mohana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry... for the explanation. It is clear for this example.

I need to practice overloaded methods with var-args primitives and Wrappers

Hima


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!