   Java typecasts
  I have a generic LinkedList of AtomicExpression objects. AtomicExpression is a parent to several other classes, for example Number and Variable. All child classes have an evaluate() method (not abstracted in the parent) that returns either a char or an int.I am trying to traverse the list with an AtomicExpression variable ('stepNode'), but am failing to access child class members and methods. I am attempting to traverse the list and fill in a String with characters representative of the nodes in the list.LinkedList<AtomicExpression> list = new LinkedList<AtomicExpression>();/* populate the list with Numbers and Variables here. */ListIterator<AtomicExpression> iter = list.listIterator();AtomicExpression stepNode = new AtomicExpression();String output = "";while(iter.hastNext()){    stepNode = iter.next();    output += stepNode.evaluate();}The line "output += stepNode.evaluate();" generates a cannot find symbol error.Basically, regardless of what class 'stepNode' currently is, I want that class's evaluate() method to return a char or an int, which can be appended to the string. What sort of typecasting needs to be done here???   
  Since all subclasses of AtomicExpression has an evaluate-method it should reallybe an abstract method in AtomicExpression. Why is it not allready?Otherwise you would have to cast each class to it's correct typ (Number etc..), which is not really recommended.   ;  Thex0,here is my class definition for AtomicExpression.class AtomicExpression{}That's all - it's just a wrapper so I can put all its children into the same collection. The problem is, because the children are all so different, their evaluate method's have different signatures. For example, one evaluate method takes a Widget1 parameter, while another might take a Widget2 parameter. Some return doubles, others return chars or ints.Quote:Since all subclasses of AtomicExpression has an evaluate-method it should reallybe an abstract method in AtomicExpression. Why is it not allready?Because I don't think I can define a single abstract method in AtomicExpression to cover the variety of function signatures evaluate() has in all of its children. If I can, I'd be delighted if someone could tell me how.More importantly, you never answered my question about the typecasting. Yes, I know typecasting is necessary. I've just never worked with this stuff before. Perhaps you can give me an example based on the code in my original post?thank you for your help guys   ;  (TypeName)Expression attempts to type Expression as Typename, causes a type cast error if Expression is not of type TypeName or one of its subtypes.EDIT: also, except for reflection, you have no automatic means to call a function by its name in Java. All calls must identify the function using its signature (not its name) because of overloading. Therefore, from the point of view of Java compiler, char execute() and int execute() do not have the same "name", because their signature is different (for instance, int-execute vs char-execute). Therefore, if you want to call one or the other, you will need to write two lines of code. One to call the first, and one to call the second.   ;  This smells like just bad design. A solution would be to create an evaluateAsString() abstract method that would at least take care of the return values, though that still leaves you with different parameters.Also, a class like AtomicExpression without any data or methods does not make any sense. This is Java, all classes already share Object as super class.You could have a look at the instanceof operator or even have a look at the reflection abilities of Java, but I still advise taking a few steps back and having a look at the problem again before you start something messy like that. The visitor pattern might be worth a try also.   ;  Okay everybody is giving me excellent suggestions here, but you are just giving me the theory...I have very little experience with Java, and virtually no experience with generics, and LinkedList traversals, and typecasting and reflection.I provided the code in my original post and it would be AMAZING if somebody could take everything that's just been recommended and show me how to modify my code as to fix the problem.This is not some humungous bug here, I think the problem should be relatively easy to fix, if someone could just shine some light on the situation for more.   ;  Here's one possibility.  Didn't know what you had in mind for your final objective, but I think this is what you're looking for.LinkedList<AtomicExpression> list = new LinkedList<AtomicExpression>();/* populate the list with Numbers and Variables here. */ListIterator<AtomicExpression> iter = list.listIterator();String output = "";while(iter.hasNext()){    AtomicExpression stepNode = iter.next();    if (stepNode instanceof Number) {  Number num = (Number) stepNode;  int num = num.evaluate();  output += num;    } else if (stepNode instanceof Variable) {  Variable var = (Variable) stepNode;  char chr = var.evaluate();  output += chr;    }};  Optus,I am born anew in your genius. Thank you!   ;Quote:Original post by hisDudenessAll child classes have an evaluate() method (not abstracted in the parent) that returns either a char or an int.Urg. The whole point is to make things behave the "same way" as seen from the outside. If all you ever do with the evaluate() results is append them to a string, you could try something like:class AtomicExpression {  virtual void appendRepresentationTo(StringBuffer sb);}class Number extends AtomicExpression {  void appendRepresentationTo(StringBuffer sb) {    sb.append(some_integer);  }}class Variable extends AtomicExpression {  void appendRepresentationTo(StringBuffer sb) {    sb.append(some_character);  }}LinkedList<AtomicExpression> list = new LinkedList<AtomicExpression>();/* populate the list with Numbers and Variables here. */ListIterator<AtomicExpression> iter = list.listIterator();// You don't need the temporary stepNode, and you definitely don't need// to initialize it to a new value.StringBuffer buffer = "";while(iter.hasNext()) {    iter.next().appendRepresentationTo(buffer);}String output = buffer.toString();This was written for C++, but is about exactly the kind of OO problem you are trying to deal with here.   