protected: if a member is protected, it's accessible to all classes in the same package (doesn't matter of it is a subclass or not)
Originally posted by Thomas Drew:
Roel this is wrong.
for a class to access a protected member of another class it has to be a child of that class. Even if two class are in the same package, if class B isn't inherited from class A class B will not be able to access any protected members of class A.
Originally posted by Sandeep Narasimhamurthy:
You are all messed up.
Any class within the same package can access protected members (variables/methods).
Sub class or not, doesn't matter. It can access protected members.
Then what's the use of protected ?
Protected comes into picture for classes in OTHER PACKAGE.
If there is a subclass in other package, it can access Protected members. Not directly, buy by declaring objects of subclass, it can access protected members of parent class.
This mechanism provides a way to access protected members from other packages. Without this, only PUBLIC members will be available to classes of other packages, inherited or not.