File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

'protected' modifier

 
jibs parap
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a subclass inside the package, the protected member can be accessed through inheritance as well as using a reference.
Is it true or false?

Thanks
 
Devi Sri
Ranch Hand
Posts: 114
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A protected member can be accessed through inheritance in the subclass , either in the same package or a different package.

But we cannot access a protected member through reference , even in the same package.
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the same package it can be accessed with super class reference.But the subclass-outside-package can't acces it through super class reference.

Just try it with simple code.

Thanks
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objection, your honor!

devisri nanubala wrote:
But we cannot access a protected member through reference , even in the same package.


Sure you can. In the same pack protected members are full visible. Check this, all three classes in the same pack (and file): referenced without inheritance.

Compiles and prints
foo!
2.2342

The difference between reference and inheritance shows only in different packages.


Yours,
Bu.
 
jibs parap
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much for the quick responses..Much appreciated.
 
Devi Sri
Ranch Hand
Posts: 114
Eclipse IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Burkhard for your correction.

I apologize for my mistake.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Devisri Nanubala wrote
Thankyou Burkhard


De nada, rancheiro.

Nothing to apoligize about.

I find the word "protected" totally misleading by the way.
...Does not have to do really with this thread but as protected is an access level keyword, one may think, that it protects the access in a way. In a way that it reduces it.
But in reality, it widens the access (rather than protecting it), i.e. to inherited members in other packages.
Example:


Frog does not compile, because of reduced visibility.

I find it just strange, that "default" is less visible than "protected". But that's only about the naming.

Bu.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic