0

Possible Duplicate:
Is `List<Dog>` a subclass of `List<Animal>`? Why aren’t Java’s generics implicitly polymorphic?

public abstract class AbstractServerSession<E extends AbstractPacket> {
[...]
packet = (E) PacketFactory.getPacket(packetClazz);

I get this error that IMHO makes no sense:

Type mismatch: cannot convert from AbstractPacket to E

How he cannot convert if I explicitly stated that E is a child of AbstractPacket?

I can add a cast

packet = (E) PacketFactory.getPacket(packetClazz);

but then of course I get this warning:

Type safety: Unchecked cast from AbstractPacket to E

So what is <E extends AbstractPacket> for?

EDIT: as requested

public static final AbstractPacket getPacket(Class<? extends AbstractPacket> clazz)

Thanks for your time!

Community
  • 1
  • 1
m0skit0
  • 25,268
  • 11
  • 79
  • 127
  • 1
    What do class `PacketFactory` and its method `getPacket` look like? – Jesper Oct 24 '12 at 09:39
  • 2
    I don't see the connect between `AbstractServerSession` and `PacketFactory`. You have shown two completely disparate pieces of code. There are no conclusions to draw. – Marko Topolnik Oct 24 '12 at 09:41
  • From the warning you can infer that getPacket() returns AbstractPacket: *Type mismatch: cannot convert **from AbstractPacket to E***. I'll put the prototype. Also I don't see this as an exact duplicate of the other question. He's using different types for the generics and here I'm casting a return value. – m0skit0 Oct 24 '12 at 09:43
  • Ok I see why now based on the duplicate, thanks Marko. I voted to close my own question, just feels weird :) – m0skit0 Oct 24 '12 at 09:46
  • Why did you vote to close your own question? Just delete it... – Mischa Oct 24 '12 at 11:15

1 Answers1

0

but then of course I get this warning: Type safety: Unchecked cast from AbstractPacket to E

I think the warning makes sense since E type can't be decided at compiling time, but it didn't impact on your running.

  • 1
    E type can't be decided at compiling time -> not totally true, it can be bound to children of AbstractPacket. – m0skit0 Oct 24 '12 at 10:09