I want to create a solution for the following problem without much redundancy:
I have an abstract class Unit, subclasses SubUnit1 and SubUnit2. All Units have a maxValue, which differs from SubUnit1 to SubUnit2, but should be the same for all instances of the same sub unit at any time.
I don’t want to copy all the setters, getters or some kind of method around these values, because they are identical for all subclasses. I also don’t want to use normal paramters and hand down the methods to subclasses and update every single instance of these subclasses if necessary. And somehow get the current value when I create a new instance.
Is there a way to declare some sort of static parameter and methods in the parent class Unit, that differ in the different subclasses?
You could define a protected constructor in the parent class that accepts the value of the MAXVALUE
Then in your derived classes you expose constructors that call the parent’s constructor with a fixed value, unique per subclass.