Hello,
I am somewhat curious about the following compile time error:
incompatible types for ?: neither is a subtype of the other
which I am getting in the following situation. Consider:
FooA fooA = new FooA(); // FooA is a subclass of Foo
FooB fooB = new FooB(); // FooB is a subclass of Foo
Foo foo;
foo = fooA == foo() ? fooA : fooB; // foo() returns an instance of Foo
This compiler error goes away when I write the somewhat longer:
FooA fooA = new FooA(); // FooA is a subclass of Foo
FooB fooB = new FooB(); // FooB is a subclass of Foo
Foo foo;
if (fooA == foo()) { // foo() returns an instance of Foo
foo = fooA;
}
else {
foo = fooB;
}
Anyone know the reason for this compiler peculiarity (using J2SE
1.5.0_06).
Thanks,
JG
Ranganath Kini - 09 Feb 2006 13:06 GMT
Tats weird, coz I ran a similar code to yours and it worked without any
problems. Here it is:
class Foo {
}
class FooA extends Foo {
}
class FooB extends Foo {
}
public class FooTest {
public static void main( String[] args ) {
FooA fooA = new FooA();
FooB fooB = new FooB();
Foo foo;
foo = ( fooA == foo() ) ? fooA : fooB;
}
static Foo foo() {
return new Foo();
}
}
Am I right?