File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why this is compile time error" Watch "Why this is compile time error" New topic

Why this is compile time error

Chiranjeevi Kanthraj
Ranch Hand

Joined: Feb 18, 2008
Posts: 290

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?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

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]
I agree. Here's the link:
subject: Why this is compile time error
jQuery in Action, 3rd edition