Results 1 to 16 of 16
  1. #1
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default Confused about Overriding - Please Help

    Hi folks

    This is the second time I've been given an assignment where overriding a method in a subclass has been stipulated, but I just can't figure out why it doesn't work. Below is some code (sniped for brevity). Can someone figure out why it doesn't work?

    From the superclass
    Java Code:
    /* instance variables */
    
       private OUColour colour;
       private int position;
       
       <snip>
       
        public int getPosition(){
          return position;
       }
       
       /**
        * Sets the position of the receiver to the value of the argument aPosition
        */
       public void setPosition (int aPosition){
          position = aPosition;
          this.update("position");
       }
    From the sub class

    Java Code:
    public class RandomFrog extends Frog{
       private Random rand = new Random();
       public int nextPosition(){
          return rand.nextInt(11) +1;
      }
      <snip>
      
       public RandomFrog(){
          super();
          super.setPosition(super.getPosition());
       }
       <snip>
        public void setPosition (int aPosition){
       if (aPosition  <0 || aPosition >11 ){
           aPosition = nextPosition(); 
          }
          else{
            aPosition = aPosition;
         }
    Now, the problem I'm experienceing here is that the setPosition() method in the subclass should provide an instance variable (position) with a value. But as this variable is declared as private in the superclass my object doesn't take in the new value (resolved by the argument aPosition) and I can't set it directly. When I send a setPosition() message to the object, I can see the argument changing value but not the variable (which is set in the superclass). This means that my object instance variable doesn't change and I can't get the object to behave properly. If I create a new method that does the same thing, but I include the message
    Java Code:
    this.setPosition(aPosition)
    it will work. I obviously can't include this line in the overriding method as it will cause a stack overflow.

    Any idea what I'm doing wrong? I thought the way overriding worked would be that the subclass method would be invoked, but everything else would work as expected.

    Thanks

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    public class RandomFrog extends Frog{
       private Random rand = new Random();
       public int nextPosition(){
          return rand.nextInt(11) +1;
      }
      <snip>
      
       public RandomFrog(){
          super();
          super.setPosition(super.getPosition());
       }
       <snip>
        public void setPosition (int aPosition){
       if (aPosition  <0 || aPosition >11 ){
           aPosition = nextPosition(); 
          }
          else{
            aPosition = aPosition;//what is this supposed to do?
         }
    perhaps I am missing something, but, in the overridden setPosition, it takes the argument aPosition, and then it assigns that variable, which isn't defined anywhere else, to either nextPosition() or aPosition.

    Declare a variable in the subclass named aPosition and then do the following:
    Java Code:
    public void setPosition(int aPosition){
      if(aPosition < 0 || aPosition> 11){
        this.aPosition = nextPosition();
      }
      else{
        this.aPosition = aPosition;
      }
    }
    I'm not sure what the goal of this is, are you trying to set the position in the superclass from the subclass?
    Last edited by sunde887; 01-24-2011 at 02:32 PM.

  3. #3
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Hi sunde887

    It's actually the other way around. The subclass inherets the attributes from the superclass. But in my subclass I should override the setPosition() method so that I can determin it's new value using the new logic.

    I appreciate that declaring a new variable (position) within the subclass would allow the logic to work, but there are two problems with this (trust me, I've tried).

    First, the object constructed from the subclass would have two attributes called position, both with difference values. The one inherited from the superclass would have 0 (the default) and the new one would have the value ascribed from the overriding method. Second, I would also have to override the method getPosition() which returns the value of the variable position. Both of these are outside the scope of the assignment. Strangly, all the litrature I have suggests that my method should work, but I'm obviously not getting it right. The new overriding method should pass the argument back up the chain to populate the variable (position) which allows the getter method getPosition() to return the value of the variable.

    Confused? I know I am.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Try switching the superclass variables to protected instead of private, I don't believe private variables are inherited in the subclass.
    http://download.oracle.com/javase/tu...ubclasses.html

    The private members are not inherited, they can still be accessed with the set and get methods but the subclass doesn't automatically receive it's own variable.

    also,
    Java Code:
     aPosition = aPosition;
    That can't work, because of scope you are setting the argument to be equal to itself.
    Last edited by sunde887; 01-24-2011 at 03:05 PM.

  5. #5
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Normally I would agree, and if I do that I would expect it to work, however(and there is always a however) this is again outwith the scope of the assignment. In fact, the litrature I have expresses more that once that changing the variable in that way is usually forbidden as this can cause issues elsewhere within the project.

    So the overriding method should only affect the attributes of the subclass and the attributes of the superclass should remain unaffected.

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Is this a large assignment? Can you post the full code and the assignment?

  7. #7
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Sure.

    Just a point though. Everything your saying reflects my (still fledgling) understanding of how Java works. When I came across this issue before a friend of mine who is a fully fledged sun certified java developer that get's paid to write this code agreed with you. At first I thought it was a mistake with part of the assignment (it's broken up into 4 parts which aren't related to each other) but they have stated the same thing again. Can't be a mistake twice.

    I'm about three quarters of the way through the code. To overcome this issue I've created a new method that does what I need it to do in the meantime. Once I've completed the whold thing I'll post the entire code.

    Thanks again for you help

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    No problem, I am still a beginner as well, other people here may be able to help better, but I believe you need the subclass to have it's own position variable which the overridden method can then change.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Why can't you use setPosition() on the parent, again?
    I didn't understand why not.

    I mean, you want to set the position attribute, and setPosition() is how you do that.

  10. #10
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Why can't you use setPosition() on the parent, again?
    I didn't understand why not.

    I mean, you want to set the position attribute, and setPosition() is how you do that.
    HI Tolls

    The parent should be left as is, i.e. the logic for setPosition() in the parent class should be left alone. It's only when constructing the child class (in this case the RandomFrog) that the new logic should apply so that setPosition() is different for that object type only.

    Think about it like this. I have two child classes that extend the superclass. One called Frog and the other called RandomFrog. Frog sets it's position directly by calling the method of the superclass and setting the value of the variable to 1
    Java Code:
    this.setPosition(1)
    . However, the other child class of type RandomFrog should set it's position to a random intiger between 1 and 11. To achieve this, I should override the setPosition() in the RandomFrog object only. This way Frog and the superclass objects set their positions using the logic therein, only RandomFrog works out the position in a different manner.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    I know that, but you still need to give the position attribute a value, and the only way to do that is to call super.setPosition() in you child classes setPosition() method, at the end.

    Pass in the new aPosition, after it's been checked.

  12. #12
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    I know that, but you still need to give the position attribute a value, and the only way to do that is to call super.setPosition() in you child classes setPosition() method, at the end.

    Pass in the new aPosition, after it's been checked.
    Do you mean this?

    Java Code:
    public void setPosition (int aPosition){
       if (aPosition  <0 || aPosition >11 ){
           aPosition = nextPosition(); 
          }
          else{
            aPosition = aPosition;
         }
    	 super.setPosition();
    	}

  13. #13
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    I know that, but you still need to give the position attribute a value, and the only way to do that is to call super.setPosition() in you child classes setPosition() method, at the end.

    Pass in the new aPosition, after it's been checked.
    Tolls, you are a STAR!!. Thank you so much. I get it now.:D

    Working code below:

    Java Code:
     public void setPosition (int aPosition)
      {
       if (aPosition  <0 || aPosition >11 ){
           aPosition = nextPosition();
               }
          else{
            aPosition = aPosition;
             }
         super.setPosition(aPosition);
       
          }
    Like everything else, it's easy when you know how. Cheers again.

  14. #14
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Style and neatness are important. Developing good and consistent habits will help make your code more readable and will even help you to think more clearly.
    Java Code:
    public void setPosition(int aPosition)
    {
            if (aPosition  < 0 || aPosition > 11 ) {
                    aPosition = nextPosition();
            }
    [COLOR="Red"]        else {
                    aPosition = aPosition;
            }
    [/COLOR]        super.setPosition(aPosition);
    }
    The code in red is superfluous -- get rid of it. Note the indentation and spacing.

    -Gary-

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Quote Originally Posted by jazzermonty View Post
    Tolls, you are a STAR!!. Thank you so much. I get it now.:D
    That's great.
    (see note above about neat formatting etc)

  16. #16
    jazzermonty is offline Member
    Join Date
    Jan 2011
    Posts
    71
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    Style and neatness are important. Developing good and consistent habits will help make your code more readable and will even help you to think more clearly.
    Java Code:
    public void setPosition(int aPosition)
    {
            if (aPosition  < 0 || aPosition > 11 ) {
                    aPosition = nextPosition();
            }
    [COLOR="Red"]        else {
                    aPosition = aPosition;
            }
    [/COLOR]        super.setPosition(aPosition);
    }
    The code in red is superfluous -- get rid of it. Note the indentation and spacing.

    -Gary-
    Thanks Gary

    Duly noted and tested. Seems so simple now. Thanks for all your help folks.

Similar Threads

  1. overriding v/s hiding
    By parminder in forum New To Java
    Replies: 3
    Last Post: 01-17-2011, 04:24 AM
  2. overriding with same code
    By jomypgeorge in forum New To Java
    Replies: 4
    Last Post: 12-31-2010, 06:54 AM
  3. Overriding
    By prasanna1157 in forum New To Java
    Replies: 7
    Last Post: 09-07-2010, 07:47 AM
  4. Overriding
    By renuka_renukut in forum Advanced Java
    Replies: 3
    Last Post: 05-21-2010, 08:45 AM
  5. Method Overriding - Seriously confused :-(
    By fullmetaljacket in forum New To Java
    Replies: 4
    Last Post: 05-26-2009, 04:18 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •