Forums Register Login

getter not returning subclass instance variable values set by setter

+Pie Number of slices to send: Send
Hi, I'm playing around trying to become familiar with getters and setters, but when I attempt to set and retrieve values for variables in the subclasses "Lion" or "Person" of the superclass "creature" I end up getting back the default values from the superclass rather than the subclasses that I'm attempting to call.


Here is the code that does the calling:



...and here is the code from the superclass and one subclass (other subclass code is pretty much the same)...



The output I get is:

I am a lion. My position is 31
null position is 0
Grr. Arrgh. *SWAT*
I am a human. My position is 11
null position is 0
Have at thee, swab!

So, I'm relatively certain I'm calling the correct subclasses since the correct methods are used and report that the variables have changed when reported by the called methods themselves, but I just can't seem to get those values back to the calling method via "getting" the variable's value or directly assigning it (the variables p and q get "0" and "null" rather than the subclass values).

Any help would be greatly appreciated.
1
+Pie Number of slices to send: Send
species is defined in both creature and Lion. I have not copied you code into my environment, but since you are iterating a as a creature, then my expectation is that you are accessing species as creature and as such has the value related to creature which is null (unset).

My recommendation is to set the species value via a creature setter (setSpecices) or constructor and call/reference it from the Lion class and eliminate species as a variable from Lion. Similarly, I would recommend you access the species variable via a getter (getSpecies) also created under species, but that is just more of an opinion/style of my perference as opposed to something that I think will impact your problem.
+Pie Number of slices to send: Send
Ah. Thanks. With your help I was able to tinker and get it working. Calling a "Lion" method for setSpecies to set the "creature" superclass' species variable and removing the species variable from from the "Lion" subclass fixed that issue. All I needed to do to fix the position variable problem was remove "int" from the line "int position = (int) (Math.random() * 100);" I guess that was initially creating a new "Lion" position variable rather than assigning the value to the position variable declared in the parent class?

Initially I was hoping that I'd be able to automagically assign values accessible from commands calling on the parent class by simply declaring them in the subclass, but apparently it doesn't work that way.
+Pie Number of slices to send: Send
Right, sorry, I did not delve into the question of the position value. You found the answer. When you had "int position" in the method you declared a new variable local to the method and used it. By removing the int, you then "scoped" to the class variable to within creature and it was set right. By remoting position from Lion you'd remove the second declaration. Also note, defining the position get/set in creature and again in Lion is valid (you override the method as it applies to Lion). Whether you want to do this or not is up to you. In the example you have get/setPosition defined in Lion the same as creature so you don't really need it (and you have the same issue as before in that you have a position variable in lion and creature).

Regarding declaration, you kind of shot yourself in the foot by delcaring it as you've found. By declaring it you ended up with two copies of the variable with different contexts.
+Pie Number of slices to send: Send
You cannot override fields, as this FAQ makes clear. You ought not to use the same name field in a subclass as in a superclass; you ought to provide access via public getXXX methods (and maybe setXXX too); then there is no need even to mention that field in a subclass.
Having a class called Lion with a species field whose value is "Lion" strikes me as dubious class design. You can get it to return "Lion" with return this.getClass().getSimpleName(); as an alternative if you call the class Lion.
Sasparilla and fresh horses for all my men! You will see to it, won't you tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4421 times.
Similar Threads
Adding elements to a vector
A subclass object would invoke superclass constructor?
error....
The method output
Extend ArrayList?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:33:48.