I am writing an abstract class, and I have a variable that needs to be defined by a subclass (in this case, it is the int that is used in the superclass). I cannot decide whether to define a variable protectedwith a default value, and let the subclass change the value either in the constructor or through the method setter. Or define an abstract method in the parent class, so that all subclasses must implement it and return the value they want to use (then access the method in the superclass using the abstract method). Can someone tell me if there are any good reasons why one of the methods should be preferable to the other?
Abstract Method:
public abstract int getValue();
public int getValue() {
return 5;
}
Subclass to reflect on this value and choose what it wants.
, , , . ( , , )
:
protected int value;
public SubClassImpl() {
value = 5;
}
, , .
, .