incompatible types for ?: neither is a subtype of the other

J

John Goche

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
 
R

Ranganath Kini

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top