• 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 confusion

 
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


in the above code, 'i' is private to class abc, and method show() is public which is accessing private member i. Now class pqr extends class abc, and so it inherits the public method show() but not the private member i. Buut still, an object of pqr can access the variable i through the show() method. Isnt this contradictory to the definition of 'private'?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the access is via the abc class. By giving the method public access you have allowed access to the show method, and the show method can access i.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you extend a class with a public method in, the method remains public. Once access has been allowed, you can't restrict it. Otherwise your pqr object ISN'T-An abc object.
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont get it, what i used to think is, once a class has been inherited everything that can be inherited (depending on the access specifiers) gets included in the subclass definition and the private things are completely invisible. So now, this code proves that when an object of type pqr is instantiated, memory WILL be allocated for the varaible 'i' although it is not inherited!!!I hope thats not too confusing..
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable 'i' is still part of a pqr object because a pqr object is also an abc object.

Look at this structure:



Even though x is declared private in the Parent, the child object still needs needs (and has) the value. Otherwise calling getX() on a Child would not work. And in your code, the show() method would not work. It uses the 'i'. While the pqr class cannot directly access 'i', it still exists within it since it is part of an abc object and is used by pdr when its doing abc stuff.

If I wanted to override something in the Child object, say that x is always set to three times the passed in value, I can do this:


Does that help?
[ August 18, 2008: Message edited by: Mark Vedder ]
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So does this mean that whenever a subclass is instantiated, memory will be allocated even for the private members of the superclass even though they are not logically a part of the subclass...or are they?!
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<ya thanks mark, deleted the stray post..>
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Janardan Kelkar:
So does this mean that whenever a subclass is instantiated, memory will be allocated even for the private members of the superclass



Yes

Originally posted by Janardan Kelkar:
even though they are not logically a part of the subclass...or are they?!


They are part of the subclass. Even though the subclass does not have direct access to them, they are part of the subclass. A subclass is also a superclass. This allows me to do this:


Therefore if the superclass has it, the subclass has it as well.


For example, in your original code, while pqr does not have direct access to 'i', it still has an 'i' so when it is doing abc stuff, such as "show()", it can do that.
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, i think i get it, but now, would it be good programming practice to use private members of the superclass in a subclass?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Janardan Kelkar:
ok, i think i get it, but now, would it be good programming practice to use private members of the superclass in a subclass?

No.

You do not have direct access to the superclass' private members. In the example you showed, there was access to a public method which could gain access to the private members.
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes of course, so i cannot declare any new methods in the subclass that would access the private members of the superclass, but that is my question! I think it would not be ethical enough to write functions in the superclass that manipulate the private data and then use them in the subclass, right?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Janardan Kelkar:
I think it would not be ethical enough to write functions in the superclass that manipulate the private data and then use them in the subclass, right?

Of course you can. If you have a public method which manipulates private fields in the superclass, you can use that method anywhere. It automatically reappears in the subclasses (unless overridden), because a subclass IS-A superclass.
[ August 18, 2008: Message edited by: Campbell Ritchie ]
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes, i know we CAN, what i am asking is SHOULD we?!i mean, is it good code?
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course it can be good code. Look at Mark's example with super.setX(3 * x);
 
Janardan Kelkar
Ranch Hand
Posts: 72
Eclipse IDE Firefox Browser Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, thanks a lot, i think i understand...
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done. It is always good to see people getting an understanding of the subject.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As quoted in "Head First Java"

We don't expose our privates in java!



 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic