• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't understand what happens to private fields when inheriting  RSS feed

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I have this class Humor, the 'h' field is private so it should mean that I can't access it from a subclass.



But then why does this work?

 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bund De wrote:But then why does this work?

Because Sad doesn't make any reference to 'h'. if it did (assuming they're in different .java files), you would get an error.

Winston
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bund De wrote:
But then why does this work?



Also, what shouldn't this work? Any class can access a public method. What should subclasses have less privileges than any other class?

Henry
 
Bund De
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok I got it thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!