public class Counter{
private int value;
public int getValue(){
return value;
}
public void setValue (int value){
this.value = value;
}
public void increment() {
/* */
}
public void decrement() {
/* */
}
}
public class LoopingCounter extends Counter{
private int limit;
public LoopingCounter(int limit){
this.limit = limit;
}
public void increment(){
int value = super.getValue();
if( value == limit){
System.out.println("Value has reached the limit, cannot increment any further!");
setValue(0);
} else{
value++;
setValue(value);
}
}
public void decrement(){
int value = super.getValue();
if(value == limit){
System.out.println("Value has reached the limit, cannot decrement any further!");
} else{
value--;
setValue(value);
}
}
public int getValue() {
System.out.println("override");
return 1000;
}
}
public class CounterTest{
public static void main(String[] args){
LoopingCounter lc = new LoopingCounter(100);
for(int i=0; i<150; i++){
lc.increment();
System.out.println(lc.getValue());
}
}
}
In this case, the LoopingCounter is supposed to trigger the getValue
method in Counter class. But for some reason when I run it, it keeps using its own getValue
method.
Please help me understand why I cannot call the parent method this way.
Apologies:
I now see my mistake. I apologize. I did not realize the lc.getValue() and was puzzled why the lc.increment failed to call the super.getValue() properly. Morale of story is get enough sleep before posting in SO. -_-"