This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why this is compile time error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why this is compile time error" Watch "Why this is compile time error" New topic
Author

Why this is compile time error

Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 289

Hi all

if i have a method like



i can call someMethod(null)

But if i have the



i cant call someMethod(null) which gives compile time error


Please explain why?
Thanks


-Chiru
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

The compiler will choose the "most specific" method. Because String is a subclass of Object, there's a clear choice between the first two methods; a subclass is deemed to be "more specific" than the superclass. But in your second example, String and StringBuffer are both subclasses of Object, so neither is more specific than the other; there's no clear choice, so it's a compile-time error.


[Jess in Action][AskingGoodQuestions]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why this is compile time error
 
Similar Threads
Throwing Exceptions
Whats wrong with the protected method?
Generics Doubt
Method Overload Q
Compile Error Vs runtime Error