Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Question About Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question About Inheritance" Watch "Question About Inheritance" New topic
Author

Question About Inheritance

Brandon Golway
Greenhorn

Joined: Oct 19, 2010
Posts: 23
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.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

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

Yes that would be helpful to see how you have done it.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Gary Ba
Ranch Hand

Joined: Oct 23, 2009
Posts: 161

Are you calling setAge() first and then getAge()? Let us see the main.

Gary


star chaser..
Bharath Raja
Ranch Hand

Joined: Jan 21, 2009
Posts: 111
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..


Life is either daring something or nothing - Helen Keller
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 281



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.


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Brandon Golway
Greenhorn

Joined: Oct 19, 2010
Posts: 23
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.
Martin Caminoa
Greenhorn

Joined: Oct 25, 2010
Posts: 5
Where you call setAge() method from King?
Steve Harney
Greenhorn

Joined: Jun 19, 2010
Posts: 19
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

Joined: Oct 19, 2010
Posts: 23
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

Joined: Mar 24, 2008
Posts: 3855

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

Joined: Oct 19, 2010
Posts: 23
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.
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
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

Joined: Oct 23, 2009
Posts: 161


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

Joined: Oct 19, 2010
Posts: 23
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.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39815
    
  28
Brandon Golway wrote: . . . sent me a PM suggesting . . .
Please, everybody, don't put that sort of information in an e-mail or PM.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question About Inheritance