0

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?

1
  • 8
    The whole point of polymorphism is that you don't need to know which subclass you have. You use method overrides to provide the distinguishing behaviour. Commented Jun 10, 2014 at 20:08

4 Answers 4

5

You should not need to know which child class is which, in most circumstances.

That is precisely the advantage with polymorphism.

If your hierarchical design is solid, the Node will have all the behaviors (== methods) needed to perform operations on your List items without worrying about which child class they are an instance of: overridden methods resolve at runtime.

In some cases, you might want to use the instanceof operator to actually check which child class your Node belongs to, but I would consider it a rare case, best to be avoided in general principles.

Sign up to request clarification or add additional context in comments.

1 Comment

if you are to the point you need a if(instanceof) or, more generally, a switch (type) you might consider refactoring the code to have proper polymorphism refactoring.com/catalog/replaceConditionalWithPolymorphism.html or if you cannot change the hierarchy to include the behaviour you need you can externalize that behaviour refactoring.com/catalog/replaceTypeCodeWithStateStrategy.html and have a visitor pattern accepting the node and applying the correct strategy to the correct object type dynamically.
1

Ideally you don't want to treat them differently, but if you wanted to determine the type, you can check using instanceof:

Node n = i.next(); if (n instanceof Tag) { // behavior } 

Comments

0

As others said, checking explicitly which subclass your object belongs to should not be necessary and is also a bad style. But if you really need it, you can use instanceof operator.

Comments

0

The polymorphic behavior would mean that you don't really care if you know what type of Node is it. You just simply call the API and the behavior will as per the implementation of concrete type.

But if you really need to know, one of the ways is to use instanceof to know which is the exact type. For e.g.

 if( i instanceof tag ) // handle tag else if( i instanceof data) //handle data 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.