• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
  • Himai Minh
  • Carey Brown
  • salvin francis

Isn't it supposed to give a compiler error?

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.
Posts: 689
  • 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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    Bookmark Topic Watch Topic
  • New Topic