Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes protected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "protected" Watch "protected" New topic


E Lan

Joined: Mar 15, 2004
Posts: 11
I read one book.It said that keyword "protected" is protected by inheritance and package.Does that mean just in the same package or has inheritance relation can use protected methods or variables?

package1--class A has protected static int p1;protected static p2();protected p3();
package2--class D extends A{
A a=new A();

D is inherited A,but it still has error in "a.p1=1;" and "a.p3();".
Where is my opinion wrong?
Appreciate your kindness.
[ June 14, 2004: Message edited by: E Lan ]
Jason Fox
Ranch Hand

Joined: Jan 22, 2004
Posts: 114
Protected means that the variable/method/etc in question can be accessed by other classes in the same package, and classes outside the package through inheritance. This means that if Class A extends Class B (in another package), Class A can access any of B's protected members through inheritance (that is, it can access them as if they were its own members / overwrite methods, etc) it cannot access them through dot notation:

At least, thats the way I understand it. If anyone can help clarify this point a little better, I'd appreciate it.
I agree. Here's the link:
subject: protected
It's not a secret anymore!