wood burning stoves 2.0*
The moose likes Java in General and the fly likes Private variable accessible in inherited method called using child object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Private variable accessible in inherited method called using child object" Watch "Private variable accessible in inherited method called using child object" New topic
Author

Private variable accessible in inherited method called using child object

Satej Koli
Greenhorn

Joined: Sep 05, 2012
Posts: 4
Hello guys....Let me post a code sample before asking my question:




Above code works perfectly fine...My concern is how was it possible to execute a method (viz. method0 here) using child class object when the method accessed a private variable 'a' from Parent class? I mean, method0 was inherited by child class AccessTest...and variable a was not as it is private..so I consider it as compiler will see the code for child class as if method0 was pasted in the child class.... so it was like:





If we look at the above code sample alone, we can not see variable a in child class as it was private and hence not inherited....so it should not be visible in the child class's method0...Still how did my program work?

Thanks in advance....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
Welcome to the Ranch

The private field might not be inherited, but the public method is. Because you did not override it, it does exactly the same as in the superclass.
I shall try adding code tags to your post, because it makes it look much better. Parent and method0 are not good names for classes and methods.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Private variable accessible in inherited method called using child object