• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

protected  RSS feed

 
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 ]
 
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
Boost this thread!