Originally posted by rathi ji:
... I think this will not work...
Please
test this. You'll see that it does work.
Again, if a member is protected, it will remain protected. It does not become "private" with successive inheritance. However, the meaning of "protected" is not so obvious. Section 6.6.2.1 of the
Java Language Specifications states...
"Let
C be the class in which a protected member m is declared. Access is permitted only within the body of a subclass
S of
C. In addition, if
Id denotes an instance field or instance method [and] ... the access is by a qualified name
Q.Id, where
Q is an
ExpressionName, then the access is permitted if and only if the type of the expression
Q is
S or a subclass of
S... "
Ref:
http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#62587
In
Java Rules, Doug Dunn explores this question in depth, noting that "protected instance variables ... are at once both accessible and inaccessible from subclasses declared in other packages." He further explains that "...the primary use of the protected access modifier
is not access control. The primary use of the protected access modifier is to allow subclasses to inherit instance variables and instance methods. The same instance variables and instance methods are generally thought of as being inaccessible outside of the package in which they are declared."
Ref:
http://www.javarules.com/book/sample.pdf (See Sections 2.9 and
especially 2.10.1.)
So
a protected member remains protected through successive inheritance in that the subclasses continue to (visibly) inherit the member regardless of their respective packages. However, once outside the package in which the member was declared protected, that member is generally inaccessible otherwise.
1/12: EDITED FOR CLARITY.
[ January 12, 2005: Message edited by: marc weber ]