• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private variable accessible in inherited method called using child object

 
Satej Koli
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic