wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 'protected' modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark " Watch " New topic
Author

'protected' modifier

jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
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

Joined: Dec 20, 2005
Posts: 114

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.


Devisri, SCJP 5.0, SCWCD 5.0
"Dream is Not what you see in sleep. Dream is that which never lets you sleep" - Abdul Kalam
Sireesha Mullapudi
Ranch Hand

Joined: Jun 26, 2006
Posts: 74
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

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
jibs parap
Ranch Hand

Joined: Dec 07, 2004
Posts: 134
Thanks very much for the quick responses..Much appreciated.
Devi Sri
Ranch Hand

Joined: Dec 20, 2005
Posts: 114

Thankyou Burkhard for your correction.

I apologize for my mistake.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.
 
subject: 'protected' modifier