I am trying to learn some oop in java. What I am trying to do is building a binary tree to convert an ekspresion in reverse police notation to infix notation. The problem is that I have three classes, one abstract called ArithmeticNode which represents a general node, a class called ValueNode representing a leaf with no children and the last class called OperatorNode representing a node with two children.

ArithmeticNode

abstract class ArithmeticNode 
{	
	protected Object data;
	protected String infixNotation;
	
	abstract Object printNode();
	abstract String infix();
}

ValueNode

class ValueNode extends ArithmeticNode
{
	
	public ValueNode(Number value)
	{
			super.data = value;
	}
	
	public Object printNode()
	{
			return super.data;
	}
	
	public String infix()
	{
		super.infixNotation += super.data;
		
		return super.infixNotation;
	}
	
}

OperatorNode

class OperatorNode extends ArithmeticNode
{

	Object leftChild;
    Object rightChild;
	
	public OperatorNode(Object operator, Object LC, Object RC)
	{
		this.leftChild = LC;
		this.rightChild = RC;
		super.data = operator;
	}
	
	public String printNode()
	{
		String node = new String();
		
		node = leftChild.toString();
		node += super.data.toString();
		node += rightChild.toString();
		
		return node;
	}
	
	public String infix()
	{
		super.infixNotation += leftChild.infix();
		super.infixNotation += super.data;
		super.infixNotation += rightChild.infix();
		
		
		return super.infixNotation;
	}
		

}

The problem I am having right now, is how do I know what the children in OperatorNode is an instance of. They can be both an OperatorNode or a ValueNode. Right now I have them as Object which does not work.
So the question is: is there a way to decide what instance each child is or do I need to restructure?

This article has been dead for over six months. Start a new discussion instead.