Results 1 to 16 of 16
- 01-24-2011, 02:20 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
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 superclassFrom the sub classJava 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"); }
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 messageJava 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; }it will work. I obviously can't include this line in the overriding method as it will cause a stack overflow.Java Code:this.setPosition(aPosition)
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
- 01-24-2011, 02:30 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.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? }
Declare a variable in the subclass named aPosition and then do the following:
I'm not sure what the goal of this is, are you trying to set the position in the superclass from the subclass?Java Code:public void setPosition(int aPosition){ if(aPosition < 0 || aPosition> 11){ this.aPosition = nextPosition(); } else{ this.aPosition = aPosition; } }Last edited by sunde887; 01-24-2011 at 02:32 PM.
- 01-24-2011, 02:54 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
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.
- 01-24-2011, 02:59 PM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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,
That can't work, because of scope you are setting the argument to be equal to itself.Java Code:aPosition = aPosition;
Last edited by sunde887; 01-24-2011 at 03:05 PM.
- 01-24-2011, 03:07 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
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.
- 01-24-2011, 03:10 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Is this a large assignment? Can you post the full code and the assignment?
- 01-24-2011, 03:17 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
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
- 01-24-2011, 03:21 PM #8
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 01-24-2011, 03:43 PM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
- 01-24-2011, 04:23 PM #10
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
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. 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.Java Code:this.setPosition(1)
- 01-24-2011, 04:26 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
- 01-24-2011, 04:56 PM #12
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
- 01-24-2011, 05:03 PM #13
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Tolls, you are a STAR!!. Thank you so much. I get it now.:D
Working code below:
Like everything else, it's easy when you know how. Cheers again.Java Code:public void setPosition (int aPosition) { if (aPosition <0 || aPosition >11 ){ aPosition = nextPosition(); } else{ aPosition = aPosition; } super.setPosition(aPosition); }
- 01-24-2011, 05:11 PM #14
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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.
The code in red is superfluous -- get rid of it. Note the indentation and spacing.Java Code:public void setPosition(int aPosition) { if (aPosition < 0 || aPosition > 11 ) { aPosition = nextPosition(); } [COLOR="Red"] else { aPosition = aPosition; } [/COLOR] super.setPosition(aPosition); }
-Gary-
- 01-24-2011, 05:14 PM #15
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
- 01-24-2011, 05:20 PM #16
Member
- Join Date
- Jan 2011
- Posts
- 71
- Rep Power
- 0
Similar Threads
-
overriding v/s hiding
By parminder in forum New To JavaReplies: 3Last Post: 01-17-2011, 04:24 AM -
overriding with same code
By jomypgeorge in forum New To JavaReplies: 4Last Post: 12-31-2010, 06:54 AM -
Overriding
By prasanna1157 in forum New To JavaReplies: 7Last Post: 09-07-2010, 07:47 AM -
Overriding
By renuka_renukut in forum Advanced JavaReplies: 3Last Post: 05-21-2010, 08:45 AM -
Method Overriding - Seriously confused :-(
By fullmetaljacket in forum New To JavaReplies: 4Last Post: 05-26-2009, 04:18 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks