Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why an compile error for this code?

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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{
}
 
Angel Dobbs-Sciortino
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What error are you getting when you compile? Try Tester c = new SubTester ();
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic