You cannot make token static in the base class, because then there will be one token for all the inherited classes. You need to make it an instance variable. You cannot even set it in a static method, because static methods in Java are not overridden.
In Java, variables are not overrides. If you inherit a class and add a variable with the same name, the variable in the derived class will be hidden, rather than overriding the value in the base class.
To fix this, create a token a abstract method in the database, specify the implementation in the derived class, and return the required value. If you do this, you can replace the abstract class with an interface, because there would be no variables or implementations in it:
interface Operation { // Basic operation class String getToken(); double operate (double x, double y); } class AddOp implements Operation { public String getToken() {return "+"; } public double operate (double x, double y) { return x+y; } }
Alternatively, leave it unassigned in the database, add a constructor that takes the value of the token, and assign it there:
abstract class Operation { // Basic operation class public final String token; abstract public double operate (double x, double y); protected Operation(String token) {this.token = token; } } class AddOp extends Operation { public AddOp() { super("+"); } public double operate (double x, double y) { return x+y; } }
source share