1
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. -_-"

Kim Stacks
  • 10,202
  • 35
  • 151
  • 282
  • Sth like this was already answered, take a look here: http://stackoverflow.com/questions/1032847/can-java-call-parent-overridden-method-in-other-objects-but-not-subtype – Xavjer Apr 15 '13 at 10:06
  • `LoopingCounter` defines it's own `getValue()` method. If you weren't expecting it to be invoked, what are you trying to achieve? – NilsH Apr 15 '13 at 10:09
  • Are we talking about the calls to `super.getValue()` not working in the `increment` and `decrement` methods, or the call to `lc.getValue()` not working in your `CounterTest` class? If it's the latter then it's "not working as expected" because you don't understand how it actually works. – Anthony Grist Apr 15 '13 at 10:11
  • What is not working in that code ? Everything is working as expected. – AllTooSir Apr 15 '13 at 10:19
  • I think even if you were able to find a way to invoke super.getValue(),it will return nothing because super class field value has not been set a value to return. – vinoth Apr 15 '13 at 10:25

5 Answers5

1

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 .

AllTooSir
  • 48,828
  • 16
  • 130
  • 164
1

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.

commit
  • 4,777
  • 15
  • 43
  • 70
0

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.

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
0

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.

Yahya Arshad
  • 1,626
  • 2
  • 19
  • 34
0

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.

fthiella
  • 48,073
  • 15
  • 90
  • 106
piyush
  • 139
  • 3