• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected Trouble

 
Arun Giridharan
Ranch Hand
Posts: 290
Debian Fedora Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Protected Access Specifier can used in another package if it gets extended right,so why can't i access in the following code.
[Please tell the reason(instead saying it's java rule)]


Thank you
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your example itself gives the reason. Members marked as protected are only inherited to the sub classes. You cannot use an instance of that class to access a protected member anywhere.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If this is possible. then what would be the difference between public and protected then ?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijitha Kumara wrote:Your example itself gives the reason. Members marked as protected are only inherited to the sub classes. You cannot use an instance of that class to access a protected member anywhere.


Protected members are accessible within the same package.
 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Protected members are accessible within the same package

Of course, my statement appeared that it isn't though the OP was referring to two different packages.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, he is using two packages But it would have been nice to see imports which would not throw compiler errors.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic