This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Isn't it supposed to give a compiler error?  RSS feed

 
Vector Sanasam
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The instance method display() of the superclass, SUP is accessing its private String variable s. The subclass, SUB does not inherit the private variable s, but new SUB().display(); compiles and runs fine.
I am confused about this. Please help.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that is perfectly ok. If it wasn't allowed for a class to access its own private members from public or protected methods then private members would be completely useless.

The subclass is not directly accessing the private variable. In fact it isn't even aware it exists. It simply calls the method and lets it do it's thing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!