Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Instance Variable does't work in extended class?

  1. #1
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Instance Variable does't work in extended class?

    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).
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Instance Variable does't work in extended class?

    The errors are in the nextValue() method in the subclass
    You forgot to post the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Instance Variable does't work in extended class?

    i'm not too sure if you can increment cur like the way you did in your super class... that kind of syntax works in the c/c++ languages

  4. #4
    Member
    Join Date
    Aug 2012
    Posts
    67
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Instance Variable does't work in extended class?

    Thanks a lot guys, for some reason the program ran just fine after I saved it for some reason...

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Instance Variable does't work in extended class?

    Quote Originally Posted by C++kingKnowledge View Post
    i'm not too sure if you can increment cur like the way you did in your super class... that kind of syntax works in the c/c++ languages
    Prefix/postfix increment/decrement operators work in Java as they do in C/C++

Similar Threads

  1. What is the difference between instant variable and instance variable?
    By avistein in forum Java Theory & Questions
    Replies: 7
    Last Post: January 6th, 2013, 11:42 PM
  2. Replies: 2
    Last Post: October 31st, 2012, 01:07 AM
  3. Creating an open-ended class that can be extended:
    By dedshaw1612 in forum Object Oriented Programming
    Replies: 2
    Last Post: March 9th, 2012, 09:34 AM
  4. Replies: 5
    Last Post: November 16th, 2011, 11:22 AM
  5. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 03:24 AM