Note that there are some explanatory texts on larger screens.

plurals
  1. POjava super not working as expected
    primarykey
    data
    text
    <pre><code>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&lt;150; i++){ lc.increment(); System.out.println(lc.getValue()); } } } </code></pre> <p>In this case, the LoopingCounter is supposed to trigger the <code>getValue</code> method in Counter class. But for some reason when I run it, it keeps using its own <code>getValue</code> method.</p> <p>Please help me understand why I cannot call the parent method this way.</p> <p>Apologies:</p> <p>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. -_-"</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload