GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Inheritance confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Inheritance confusion" Watch "Inheritance confusion" New topic
Author

Inheritance confusion

Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72



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'?


When the compiler's not happy, ain't nobody happy.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: May 05, 2008
Posts: 72

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..
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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

Joined: May 05, 2008
Posts: 72

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

Joined: May 05, 2008
Posts: 72

<ya thanks mark, deleted the stray post..>
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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

Joined: May 05, 2008
Posts: 72

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
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: May 05, 2008
Posts: 72

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
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
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

Joined: May 05, 2008
Posts: 72

yes, i know we CAN, what i am asking is SHOULD we?!i mean, is it good code?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Of course it can be good code. Look at Mark's example with super.setX(3 * x);
Janardan Kelkar
Ranch Hand

Joined: May 05, 2008
Posts: 72

ok, thanks a lot, i think i understand...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Well done. It is always good to see people getting an understanding of the subject.
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
As quoted in "Head First Java"

We don't expose our privates in java!




be a well encapsulated person, don't expose your privates, unless you public void getWife()!
 
GeeCON Prague 2014
 
subject: Inheritance confusion