• 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

Inheritance problem

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?








 
reply
    Bookmark Topic Watch Topic
  • New Topic