• 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 ...
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

protected  RSS feed

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();

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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!