Understanding Class Variable Inheritance

I am creating a Challenge24Solver class in Java. The logic itself works and finds solutions, as one would expect (with an arbitrary number of arguments). In any case, this part of the projects works as I expect.

The question arises from problems with the presentation of solutions. In fairness, itโ€™s worth saying that I completed this project in Python and decided to try in Java as a kind of introduction, which can be a problem, that I am also trying to do it like Python.

Here are some of my classes:

 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); } } 

The problem is that it still gets the token from the Operation class: "null"

I understand that this is probably because the statement is declared as an Operation, but I donโ€™t understand why it does not pass the standard inheritance: instance, class, each superclass

How can I change the order of the script so that tokens of more specific operations are used?

+4
source share
2 answers

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; } } 
+3
source

Static variables are referenced through the class. You defined operator as type Operation , therefore operator.token refers to Operation.token .

I recommend using a getter for this purpose:

 abstract Operation { \\ Basic operation class abstract public double operate (double x, double y); abstract public String getToken(); } class AddOp extends Operation { public double operate (double x, double y) { return x+y; } public String getToken() { return "+"; } } 
+3
source

Source: https://habr.com/ru/post/1486523/


All Articles