| Author |
Why an compile error for this code?
|
Chandra Bairi
Ranch Hand
Joined: Sep 12, 2003
Posts: 152
|
|
why is there a compile time error for the following code class Tester { void test(Object s) { System.out.println ("Object version"); } void test(Tester s) { System.out.println ("Tester version"); } void test(SubTester s) { System.out.println ("SubTester version"); } // Not compilable any more if you uncomment the line // since String and Tester are siblings void test(String s) { System.out.println ("String version"); } public static void main (String args[]) { Tester c = new Tester (); // Ambiguous, the most specific one which fit will be call c.test (null); // SubTester version c.test (new Object()); // Object version } } class SubTester extends Tester{ }
|
Thanks,
Shekar
|
 |
Angel Dobbs-Sciortino
Ranch Hand
Joined: Sep 10, 2003
Posts: 101
|
|
|
What error are you getting when you compile? Try Tester c = new SubTester ();
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
This is your error? That's an interesting example. The compiler gives this error when null doesn't give it enough information to decide which method to call. Introduction of the String method makes two classes at the "leaf" level of specificity, not what I'd call siblings but maybe peers. I actually expected the error without String, and was a bit surprised at how this worked.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
 |
|
|
subject: Why an compile error for this code?
|
|
|