File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method overloading

 
Gautam Sewani
Ranch Hand
Posts: 93
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.applet.Applet;
class Another extends Applet implements Cloneable {
public void show(Applet c)
{
System.out.println("Applet");
}
public void show(Cloneable ar)
{
System.out.println("cloneable");

}
public static void main(String ab[])
{
Another a=new Another();
a.show(a);
}
}
Why does this code produce a compile time error?
 
alex earnshaw
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gautam Sewani:

Why does this code produce a compile time error?

I'm not entirely sure...but I think you need to use a cast in the call to the show method. Object a is both an Applet and a Cloneable so the compiler has no way of knowing which method you mean to call. I think if you used

or

that it would compile (but I haven't tested it!)
Someone please correct me if I am wrong

Alex
 
Asif Masood
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Class Another is extending Applet and implementing Cloneable, so Another is of Type both Applet and Cloneable at the same time. When you call show() method by passing a Reference of type Another (which is of both Applet and Cloneable type), Compiler gets confused which version of show method to call either show(Applet) or show(Cloneable), that's y it gives compile time error.
Hope this will help to understand.
-Asif
 
John Wetherbie
Rancher
Posts: 1449
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd suggest you try Alex's suggestion and see what happens. I wrote a lot of code testing things out when I was preparing to take the Programmer test and it really helped out.
Good luck!
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem occurs because of ambiguity. The compiler sees two perfectly legitimate methods to invoke and can't determine which one it should use. Therefore, it gives an error. Here's some more details from the JLS, §15.12.2.2 Choose the Most Specific Method:

It is possible that no method is the most specific, because there are two or more maximally specific methods. In this case:
  • If all the maximally specific methods have the same signature, then:
  • [list]If one of the maximally specific methods is not declared abstract, it is the most specific method.
  • Otherwise, all the maximally specific methods are necessarily declared abstract. The most specific method is chosen arbitrarily among the maximally specific methods. However, the most specific method is considered to throw a checked exception if and only if that exception is declared in the throws clauses of each of the maximally specific methods.
  • Otherwise, we say that the method invocation is ambiguous, and a compile-time error occurs.
  • [/list]

    I hope that helps,
    Corey
     
    Gautam Sewani
    Ranch Hand
    Posts: 93
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks!
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic