Downcasting from generic bases

Discussion in 'Java' started by Christopher Benson-Manica, Mar 14, 2007.

  1. Why isn't this legal?

    public class TestIt {
    private static class Foo<T> {
    }

    private static class Bar extends Foo<Integer> {
    }

    private static void Baz( Foo<?> foo ) {
    if( foo instanceof Bar ) { // "inconvertible types" error
    }
    }
    }

    And since that's not legal, why exactly does it become legal if the
    parameter foo is of the raw type Foo? I clearly don't understand
    generics completely and it seems perverse that I can't ask a
    superclass if it's an instance of a base class just because of the
    generic type paramters.

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Mar 14, 2007
    #1
    1. Advertising

  2. Christopher Benson-Manica wrote:
    > Why isn't this legal?
    >
    > public class TestIt {
    > private static class Foo<T> {
    > }
    >
    > private static class Bar extends Foo<Integer> {
    > }
    >
    > private static void Baz( Foo<?> foo ) {
    > if( foo instanceof Bar ) { // "inconvertible types" error
    > }
    > }
    > }
    >
    > And since that's not legal, why exactly does it become legal if the
    > parameter foo is of the raw type Foo? I clearly don't understand
    > generics completely and it seems perverse that I can't ask a
    > superclass if it's an instance of a base class just because of the
    > generic type paramters.
    >

    Generics are sometimes confusing. Here is why it gives you an error:

    private class Foo<T> {
    protected T doSomething();
    }

    private class Bar extends Foo<Integer> {
    protected Integer doSomething();
    }

    foo1 = new Foo<Integer>();
    foo2 = new Bar();
    foo1.doSomething();
    foo2.doSomething();

    Erasing the types, the first method will return Object and the second
    Integer. Then, foo2's doSomething will become ambiguous in your code.
    The return type would become ambiguous because of the cast.

    Yes, I know it seems overprotective, but much of generics is like that.
     
    Joshua Cranmer, Mar 15, 2007
    #2
    1. Advertising

  3. Christopher Benson-Manica

    Tom Hawtin Guest

    Christopher Benson-Manica wrote:
    > Why isn't this legal?
    >
    > public class TestIt {
    > private static class Foo<T> {
    > }
    >
    > private static class Bar extends Foo<Integer> {
    > }
    >
    > private static void Baz( Foo<?> foo ) {
    > if( foo instanceof Bar ) { // "inconvertible types" error
    > }
    > }
    > }


    Works for me! Are you using and old or non-Sun compiler?

    There are some confusing wildcard situations where you have nesting of
    generic types (Foo<Bar<?>>), but this code should not be a problem.

    Tom Hawtin
     
    Tom Hawtin, Mar 15, 2007
    #3
  4. Tom Hawtin <> wrote:

    > Works for me! Are you using and old or non-Sun compiler?


    No. I mean, yes, but I didn't realize it until just now. Oops! :)
    (I was using 1.5.0_06 - did the language spec change or was this
    merely a bug that was fixed in a later version?)

    --
    C. Benson Manica | I *should* know what I'm talking about - if I
    cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Mar 15, 2007
    #4
    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. =?Utf-8?B?Sm9l?=

    Upcasting/ Downcasting in VB.NET?

    =?Utf-8?B?Sm9l?=, Nov 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,849
    John Murray
    Nov 14, 2005
  2. Downcasting problem

    , Mar 17, 2006, in forum: Java
    Replies:
    4
    Views:
    10,864
    tom fredriksen
    Mar 17, 2006
  3. Stuart Golodetz
    Replies:
    6
    Views:
    462
    Chris \( Val \)
    Aug 30, 2003
  4. Michael
    Replies:
    2
    Views:
    462
    Michael
    Oct 16, 2004
  5. Random

    CType and downcasting classes

    Random, Dec 27, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    462
    Random
    Dec 28, 2006
Loading...

Share This Page