Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inheritance problem

 
Paul Chamsay
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Chamsay
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there some way this can be done without having to use set again in Subclass2 ?
 
Paul Chamsay
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?








 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic