'Create a final field initialized by a child class
I have an abstract class with subclasses that share a method that increments a value by a specific increment and is bounded by a lower limit and an upper limit. Each subclass has its own increment and its own bounds These values should all be declared final, but if I declare them final how can they be initialized by the subclasses.
Below is the only way I could think of to get it to work, but it seems rather cumbersome.
public abstract class ClassA {
protected int LOWER_LIMIT;
protected int UPPER_LIMIT;
protected int INCREMENT;
public ClassA() {
}
}
public class ClassB extends ClassA {
public ClassB() {
super();
this.LOWER_LIMIT = 0;
this.UPPER_LIMIT = 4200;
this.INCREMENT = 15;
}
}
public class ClassC extends ClassA {
public ClassC() {
super();
this.LOWER_LIMIT = 0;
this.UPPER_LIMIT = 99;
this.INCREMENT = 1;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
