Skip to my second post for a better example.
Brandon Golway wrote:
Whenever I try to print king.getAge() (assuming I created the object and everything correctly) from the main function I would get 25 and not 22 and I dont understand why.
I think you have called the getAge() within the main.. if you want to print 22 then you should call the setAge() within the main..
The Reason everyone asked to see your code was because we thought it should work, Looking at your Soldier class i can see the issue. Rather than give you the solution Ill try and point you in the right direction.. Start by comparing the age variable in the Quality class (line 4) you originally posted with that of your latest post (line 4). , they is a different and its significant. also compare the age variable between the king class and the soldier class (trick question).
I messed around with those a little while before posting this and nothing seemed to work, I originally tried private but that didnt work so I tried protected since I was told its pretty much the same as public but can only be used in the scope of sub/super classes and that didn't work either. After reading your post I changed them all to public and that got rid of the warnings that the variables are never read, yet they still return either a 0 int the case of an int or null in the case of a string.
Brandon Golway wrote:... that got rid of the warnings that the variables are never read ...
Are you talking about warnings from any IDE? And why you have same instance variables in both the classes? If you make them private in the super class you should have setter methods which let you set the values for them since private members are not inherited in to subclasses.
I would also advise you to re-examine you class hierarchy. Is Soldier really a Qualities? Or does a Soldier HAVE Qualities? Inheritance is not always the answer. When deciding whether or not you should extend one class with another one thing I usually do is ask myself "Is A really a B? Or does A have a B?" If A is a B then inheritance is probably what you want. Otherwise you might consider some different composition. When you use inheritance (Say B extends A) you are really saying that all B's ARE ALSO A's. (But the inverse is not necessarily true).
Maybe what might make more sense in your case is to have a Person superclass which has age, height, weight, and other stuff common to all People, then extend that with Soldier and make soldier have properties appropriate to a Soldier such as Armor, Weapon, etc...Then you could define separate classes for Armors and Weapons. (I know this is a bit off topic from your original question but if you're going to start off learning Java, might as well start off right. And this is more of a general object oriented programming concept that can be carried to any OO language.)
Yes I meant the warnings that Eclipse was telling me about
One more thing I want to add up from others. Since you are using an IDE (eclipse) and assuming you are familiar with it, you should try to set a breakpoint and step through the code and watch how the variables change.
thanks for the explanation about inheritance, as for the re-structuring of the classes I should rename the superclass to Person or something like that I just couldnt think of a good name for it. Besides the Soldier class, I have two more classes called King and Queen which have all the same methods as Soldier but with slight modifcations (Soldier is really the one thats different since age/height/weight/etc.. are all dynamic [given random values] and there can be multiple Soldiers, but usually only one King and Queen whos properties are static), we had also just learned about inheritance in class and this is my mid-term project so I figured it would be better (for my grade and to make the code easier) to inherit stuff. I cant really make this too complex since I have to combine my code with other peoples code later on to make a full game.
I have been messing around with debugging in another game I'm creating which is a lot more complex than this and I must say its a hell of a lot easier to debug stuff in java than it was when I was doing C++ and python coding.