The final keyword, when applied to fields in a Java class, has nothing to do with inheritance. Instead, it indicates that outside the constructor, this field cannot be reassigned.
Java considers hiding and redefining a name separately. Redefinition actually changes the observed behavior of the program at runtime by switching this function, and the name is hidden, changing the program, changing the static interpretation of which field is the link. final in relation to redefinition works only for methods, because fields in Java cannot be redefined. Using final in these different contexts is unfortunately a bit confusing, and there is no way to prevent a field from containing its name in a subclass.
If you want buildings to have different costs, one option would be to have an overridden getCost method, which is redefined differently in each derived class. Alternatively, you can have only one protected or private in the base class that saves the cost, then each subclass sets this field either directly (if it is protected ) or through the constructor of the base class (if it is a private field).
Hope this helps!
source share