Two Laptop Bag
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "method overloading" Watch "method overloading" New topic

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)
public void show(Cloneable ar)

public static void main(String ab[])
Another a=new Another();;
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


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

Asif Masood

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

Joined: Apr 05, 2000
Posts: 1449
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, § 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,

    SCJP Tipline, etc.
    Gautam Sewani
    Ranch Hand

    Joined: Apr 19, 2002
    Posts: 93
    I agree. Here's the link:
    subject: method overloading
    It's not a secret anymore!