public class Node {
public Node right;
}
public class SpecialNode extends Node {
public String specialLabel;
}
public class Testmain {
public static void main(String[] args) {
Node n1 = new Node();
Node n2 = new Node();
Node n3 = new Node();
n1.right=n2;
n2.right=n3;
// some calculations --> now n2 has to become a SpecialNode, how ???
}
}
I am realizing a single linked list with Node, where every node knows its right neighbor. I created a list: Node n1-> Node n2-> Node n3. After creating the list, it contains just elements of type Node. Now i want to tell Node n2 to be a SpecialNode in order to supply a specialLabel. After the downcast the list should look like Node n1-> SpecialNode n2-> Node n3. How can this be done?
(Note that n2 does not know its left neighbor. And note that i have to tell n2 to be special after list creation so after initializing it as a Node of the superclass, cause i need to do some calculations before deciding which node is going to be special.)