• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About overriding

 
Mallik Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The result is 50...how can a private variable in parent class be accessed in the subclass. I understand that getX() is inherited.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because it isn't accessing a private variable. It's accessing a public method, and that method happens to return (a copy of) the value held by the private variable. This is one of the points of encapsulation - you can expose values when you want, but in a controlled way.
 
Mallik Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic