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

Discussion in 'Java' started by John Goche, Feb 9, 2006.

  1. John Goche

    John Goche Guest

    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
    John Goche, Feb 9, 2006
    #1
    1. Advertising

  2. 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?
    Ranganath Kini, Feb 9, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dimitris Mandalidis

    Re: malloc & incompatible types in assignment

    Dimitris Mandalidis, Aug 31, 2003, in forum: C Programming
    Replies:
    4
    Views:
    995
    Al Bowers
    Aug 31, 2003
  2. Brian Stubblefield

    incompatible types in assignment

    Brian Stubblefield, May 25, 2004, in forum: C Programming
    Replies:
    7
    Views:
    33,609
    Michael Fyles
    May 30, 2004
  3. Dennis Schulz

    error: incompatible types in asignment

    Dennis Schulz, May 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    449
    Emmanuel Delahaye
    May 26, 2004
  4. j0mbolar

    pointers to incompatible types

    j0mbolar, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    377
    Chris Torek
    Oct 17, 2004
  5. Replies:
    3
    Views:
    794
Loading...

Share This Page