wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Inheritance problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance problem " Watch "Inheritance problem " New topic
Author

Inheritance problem

Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Hi Everyone,

I'm trying to practice some inheritance concepts and I'm stumped here.

I'm bad at explaining stuff, so I'll try to illustrate.

Here's the code of three classes:








I'm wondering why when I run it, the Subclass2 super.print() method returns two zeros instead of 1 & 2.

When I did a debug, I noticed that when obj.print() was ran in SubClass1, the private instance variables in superclass immediately becomes zero.

Can anyone explain this please? Thanks!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

When I did a debug, I noticed that when obj.print() was ran in SubClass1, the private instance variables in superclass immediately becomes zero.


What do you mean by "immediately becomes zero"? When was it set to anything else?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Sorry, by zero, I meant, when SubClass2 invokes super.print(), shouldn't it print a 1 & 2 instead of 0 & 0 since SubClass1 invoked the set methods and set the two instance variables in SuperClass to 1 & 2?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18825
    
  40

Paul Chamsay wrote:Sorry, by zero, I meant, when SubClass2 invokes super.print(), shouldn't it print a 1 & 2 instead of 0 & 0 since SubClass1 invoked the set methods and set the two instance variables in SuperClass to 1 & 2?


Keep in mind that there are two objects -- one of type SubClass1 and one of type SubClass2. They both also happen to be (ie. IS-A) SuperClass objects. Just because the SubClass1 object, changes fields in the SuperClass component of itself (somewhat bad wording), it doesn't change the SuperClass component of SubClass2 objects -- or even other SubClass1 objects.

Henry
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Oh I understand now, thanks!.. Is there any way for SubClass2 to access those fields that SubClass1 set or do I need to set them inside SubClass2 in order to do that? I'm thinking of using get, not sure how I could apply it here though..
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Is there some way this can be done without having to use set again in Subclass2 ?
Paul Chamsay
Ranch Hand

Joined: Feb 06, 2009
Posts: 36
Henry, I've been thinking about what you said and I tried to hit the books again.

Thought about this for hours and this is what I got. It works now but was just wondering if this was the only and most effective way to do it or is there some other way to do it?








 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance problem