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 Programmer Certification (SCJP/OCPJP) and the fly likes method overloading 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 » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method overloading" Watch "method overloading" New topic
Author

method overloading

Gautam Sewani
Ranch Hand

Joined: Apr 19, 2002
Posts: 93
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

Joined: Nov 05, 2001
Posts: 60
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

Joined: Dec 13, 2000
Posts: 20
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

Joined: Apr 05, 2000
Posts: 1441
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!


The only reason for time is so that everything doesn't happen all at once.
- Buckaroo Banzai
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


    SCJP Tipline, etc.
    Gautam Sewani
    Ranch Hand

    Joined: Apr 19, 2002
    Posts: 93
    Thanks!
     
     
    subject: method overloading
     
    Similar Threads
    how to create a copy of java object
    casting
    Runtime check for methods
    Exam Questions -Req. Clarification
    RHE said it wouldn't compile but it did and ran