I know that you could break down implementations of LinkedList into two categories. In one, the actuall LinkedList is a reference to the first of some linked Nodes, like this
public class LinkedList1<E>{
class Node{
E data;
Node next;
}
Node head;
}
That version is probably better, but alternatively, there's a more 'direct' version, that makes things like deleting the head (head meaning the first element from the client's perspective) a bit trickier but still possible from within the instance of LinkedList2:
public class LinkedList2<E>{
E data;
LinkedList2 next;
}
I'm curious if there's any way to create LinkedList2Child extends LinkedList2
such that the next
field is of type LinkedList2Child
, perhaps involving reflection?
EDIT: this isn't preventing me from accomplishing anything since version 1 is available--I'm just curious about the problem in general, and this is an example.
I know that you can 'hide' fields in subclasses by declaring a field with the same name as the parent's field, but this won't work if you want to use parent methods that operate on that field.