Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected

 
E Lan
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
a.p1=1;
a.p2();
A.p2();
a.p3();
}

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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic