Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding inheritance of class variables
    text
    copied!<p>I'm building a <code>Challenge24Solver</code> class in Java. The logic itself works and finds the solutions as would be expected (with an arbitrary number of arguments). Anyway that part of the projects is works as I expect.</p> <p>The question comes from problems with the representation of the solutions. It's fair to say that I've completed this project in Python and decided to attempt in Java as a sort of introduction, which may be the problem, that I'm trying to do this too like Python.</p> <p>Here are some of my classes:</p> <pre><code>abstract class Operation { \\ Basic operation class static String token; abstract public double operate (double x, double y); } class AddOp extends Operation { static String token = "+"; public double operate (double x, double y) { return x+y; } } //other operation classes SubOp, MulOp, DivOp class Challenge24Step { // represents one step in a solution Operation operator; double x, y; //operands double value; //the value of the step (x operator y) public Challenge24Step (Operation operator, double x, double y) { this.operator = operator; // constructor code; } public String toString () { return String.format("%s %s %s = %s", this.x, this.operator.token, this.y, this.value); } } </code></pre> <p>The problem is that it still gets token from the Operation class: "null"</p> <p>I understand that this is probably because operator is declared as Operation, but I don't understand why it doesn't go through standard inheritance: instance, class, each superclass</p> <p>How would I rearrange the script so that the more specific operation classes' tokens are used?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload