• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question About Inheritance

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a project to do for my midterm in my java programming class in college and I'm not sure if I understand the concept of inheritance correctly. Can I define variables in a super-class but change those variables in the sub-classes that depends on the super-class?

Skip to my second post for a better example.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

... I'll post the actual code if someone wants me to...


Yes that would be helpful to see how you have done it.
 
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you calling setAge() first and then getAge()? Let us see the main.

Gary
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Please post the code , as it looks you are not calling setAge() before calling getAge() and that's why you are getting default value you have in super class.
 
Brandon Golway
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Qualities.java


Soldier.java


Main.java


Even though the values of the variables are defined in Solder.java and I create a soldier object, the values that get returned are from Qualities.java.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where you call setAge() method from King?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Brandon,
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).

Steve
 
Brandon Golway
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Steve, Im assuming you mean the variable identifiers (correct term?).

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.
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Brandon Golway
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I meant the warnings that Eclipse was telling me about, as for setter methods and whatever ever else you said I have no idea what you mean. Im a noob when it comes to java.
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Private members (instance variables, methods) in java do NOT inherit. You have defined 2 classes each with its own instance int member named 'age' (and several others). You have overridden the public method setAge() from the parent class on Soldier but you have not overridden the getAge() method. Hence, getAge() is defined only for Qualities and it returns Qualities age. (Remember when you construct a class in Java, the superclass constructor runs first.) If you were to make the Qualities member variables have some higher access level like default, or protected, you would then NOT need to redefine them in your Soldier subclass.

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.)
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.

Gary
 
Brandon Golway
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help guys. Steve sent me a PM suggesting that I may need to use constructors for my classes and that was exactly what i needed and i was trying to accomplish it by resetting the variables since I had forgotten about constructors. Now everything works the way I want it to.

@Alex
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.

@Gary
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.

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Brandon Golway wrote: . . . sent me a PM suggesting . . .

Please, everybody, don't put that sort of information in an e-mail or PM.
 
reply
    Bookmark Topic Watch Topic
  • New Topic