I haven't written any Java code in more than 10 years. I'm enjoying it, but I don't think I get some of the details of polymorphic programming. I have an abstract Node class, which has tag and data subclasses (among others), and I store them in an ArrayList.
But when I get them out of the ArrayList via an Iterator, I get Node objects back. I'm not sure how best to deal with the objects I get back from the iterator.
Here's an example:
// initialize the list TagNode tag = new TagNode(); ArrayList<Node> list = new ArrayList<>(); list.add(tag); // And many more go into the list, some TagNodes, some DataNodes, etc. and later I use an iterator to process them:
Iterator<Node> i = list.iterator(); Node n = i.next(); // How do I tell if n is a TagNode or a DataNode? I know that I can cast to one of the Node subclasses, but how do I know which subclass to use? Do I need to embed type information inside the Node classes?