Reputation: 132
Class Structure
class Packet {}
class PacketList extends ArrayList<Packet> {}
Now I am trying to cast object of ArrayList to Object of PacketList
List<Packet> p = new ArrayList<Packet> ();
PacketList pList = p;
But it will not work. Please help me out
incompatible types: List cannot be converted to PacketList
Upvotes: 0
Views: 412
Reputation: 9405
PacketList
extended the ArrayList<Packet>
, which means newly introduced, or overriden methods that might require and depend on other artifacts only introduced in the new PacketList
version. What if it refers to instance variables only available in PacketList
? ArrayList
won't have those instance variables, although you would intend to access them through those methods ...
No, you cannot assign an instance of a superclass, to a variable of the type matching an extending class.
You work the other way around, you use the super type as the generic type to assign extending (subclassed) instances.
Upvotes: 1