Reputation: 10812
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. -_-"
Upvotes: 1
Views: 1084
Reputation: 139
Whenever you call any method through child class then it will always first call child class method and then it calls super class method.
If you want to call super class method first then you write super.getValue()
inside getValue()
method of LoopingCounter
class.
Upvotes: 0
Reputation: 1626
This is Object Oriented Programming
call Overriding if class B extends class A, class A have method void foo()
and class B also provides implementation for void foo()
method this is called overriding and if you create object of class B and invoke method foo() method of child class will be invoked.
Upvotes: 0
Reputation: 15886
From JLS 8.4.9 Overloading and JLS 8.4.8.1.
If you override parent method in you child class, and you run that method with child class instance then overridden method should be run.
So you are getting correct behavior in your program.
For calling parent overridden method with child instance you can use super
keyword.
Upvotes: 0
Reputation: 4807
Your parent method is call but as your inherited class also has getValue() method so it's called after executing parent class method. You should change your way to get value from base class.
Upvotes: 1
Reputation: 49372
The behavior is correct . If you want to call the Counter
class getValue()
, then you need to have super.getValue()
somewhere inside the LoopingCounter
class getvalue()
method . lc.getValue()
will always call getValue()
method defined inside the LoopingCounter
class as lc
is an instance of LoopingCounter
.
Upvotes: 1