Hey guys, I have a super class and a class that extends it. For some reason, the variable cur doesn't work in the extended class even though it should work just fine. The errors are in the nextValue() method in the subclass. Why is this?
Super Class:
package CF02to06Progression; /* * Code Fragment 2.2: General numeric progression class. */ /* * A class for numeric progressions. */ public class Progression { /** First value of the progression */ protected long first; /** Current value of the progression */ protected long cur; /** Default Constructor */ Progression() { cur = first = 0; } /** Resets the progression to the first value. * *@return first value */ protected long firstValue() { cur = first; return cur; } /** Advances the progression to the next value * * @return next value of the progression */ protected long nextValue() { return ++cur; // default next value } /** Prints the first n values of the progression * * @param n number of values to print */ public void printProgression(int n) { System.out.println(firstValue()); for(int i = 2; i <= n; i++) { System.out.println(" " + nextValue()); } System.out.println(); // ends the line } }
Subclass:
package CF02to06Progression; /** * Code Fragment 2.3: Class for arithmetic progressions, which inherits from the * general progression class shown in Code Fragment 2.2 * */ /** * Arithmetic progression. */ public class ArithProgression extends Progression { /** Increment */ protected long inc; // Inherits variables first and cur /** Default constructor setting a unit increment */ ArithProgression() { this(1); } /** Parametric constructor providing the increment. */ ArithProgression(long increment) { inc = increment; } /** Advances the progression by adding the increment to the current value * * @return next value of the progression */ protected long nextValue() { cur += inc; // errors here return cur; // and here } // Inherits methods firstValue() and printProgression(int). }