jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes Generics using interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generics using interfaces" Watch "Generics using interfaces" New topic
Author

Generics using interfaces

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

I want to know why the below code does not compile,



As far as i understand, I have provided a concrete implementation of "T" itself, yet its not able to accept it...


Now.. I made the following changes for it to work:



I dont think this is a good solution since my method is indeed suppressing an unchecked conversion.

However, since
ConcreteImpl ISA SomeInterface
and
T ISA SomeInterface
why do i need suppression ?


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You don't know that ConcreteImpl is the same as T. In fact when you create an instance of MyExtendedClass, it doesn't have to be a MyExtendedClass<ConcreteImpl>. It could just as well be a MyExtendedClass<AsphaltImpl>, for example. In which case T would be AsphaltImpl and not ConcreteImpl.

So you are correct to reject your code; it would throw a ClassCastException when you tried to cast your ConcreteImpl object to type T, which is AsphaltImpl.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

If your class can only add ConcreteImpl instances, the class signature should be changed:
or perhaps


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

Paul Clapham wrote:You don't know that ConcreteImpl is the same as T. In fact when you create an instance of MyExtendedClass, it doesn't have to be a MyExtendedClass<ConcreteImpl>. It could just as well be a MyExtendedClass<AsphaltImpl>, for example. In which case T would be AsphaltImpl and not ConcreteImpl.

So you are correct to reject your code; it would throw a ClassCastException when you tried to cast your ConcreteImpl object to type T, which is AsphaltImpl.


hi Paul,
I understand that T can be a AsphaltImpl or a ConcreteImpl or any other impl like a StoneImpl or a DirtImpl
However,
Since T extends SomeInterface,

all the AsphaltImpl, ConcreteImpl, StoneImpl, DirtImpl are basically SomeInterface


check out this other example based on what you are saying:




Is there any way that i can avoid this suppress ?
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

Rob Spoor wrote:If your class can only add ConcreteImpl instances, the class signature should be changed:
or perhaps


Hmm this is not the case,
I would always want to use the interface as shown in the example above, the implementation can be any class...
i would rather not want a suppress or a cast in the code.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 917

a little more refined:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

In your own example you show why this will go wrong. X cannot be both ConcreteImpl and RockImpl; therefore, one of these casts is invalid. I see two solutions to your problem:

1) Make the generic type SomeInterface, as you clearly want to allow all implementations:

2) Don't use generics at all:
In both cases will MyExtendedClass be allowed to add any implementation of SomeInterface.
 
Don't get me started about those stupid light bulbs.
 
subject: Generics using interfaces
 
Similar Threads
A member interface is implicitly static
Help with Java Generics...
ClassCastException ?
Serialization of Interface Reference for an Implementation Class fails at De-serialization
in what ways can we change this formula..