posted 22 years ago
This depends a bit about how much you know about the access modifiers overall.
The pure differens between protected and public is that public doesn't put any restrictions on the variable/method. If you can reference the class (or subclass thereof) you can access the variable/method.
Setting something to protected means that you restrict the access some. Now only classes in the same package or subclasses of the current class can access it.
Example:
package MyPack;
class A {
{access modifier} doSomething();
//other methods
}
class B {
//code
}
--
package OtherPack;
//some stuff
class C extends A {
//code
}
--
package thirdPack;
//Some stuff
class D {
}
If you look at the classes above, you noticed that I omitted the access modifier on method doSomething(). If we were to put public there, all four classes (A,B,C,D) will be able to access it, as long as they know about class A (which A obviously do, it's called 'this').
If instead we put protected on it, only A,B & C will be able to access it. A because it's the same class (and same package), B because it's the same package, and C because it's a sub-class of A.
Hope that helped
/Mike
[This message has been edited by Mikael Jonasson (edited May 17, 2001).]