Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Downcasting from generic bases

Reply
Thread Tools

Downcasting from generic bases

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      03-14-2007
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.
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      03-15-2007
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.
 
Reply With Quote
 
 
 
 
Tom Hawtin
Guest
Posts: n/a
 
      03-15-2007
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
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      03-15-2007
Tom Hawtin <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
CType and downcasting classes Random ASP .Net 2 12-28-2006 10:16 PM
Downcasting problem parkarumesh@gmail.com Java 4 03-17-2006 10:57 AM
Upcasting/ Downcasting in VB.NET? =?Utf-8?B?Sm9l?= ASP .Net 3 11-14-2005 08:48 PM
Messy static Downcasting, any alternatives?? Michael C++ 2 10-16-2004 03:28 AM
Multiple and virtual inheritance, and downcasting Stuart Golodetz C++ 6 08-30-2003 02:31 AM



Advertisments