Help to understand the issue with protected method

前端 未结 6 1217
野的像风
野的像风 2021-01-12 00:54

I\'m reading Sybex Complete Java 2 Certification Study Guide April 2005 (ISBN0782144195). This book is for java developers who wants to pass java certification.

6条回答
  •  甜味超标
    2021-01-12 01:28

    From The Java Language Specification:

    6.6.2.1 Access to a protected Member

    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, then:

    • If 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.
    • If the access is by a field access expression E.Id, where E is a Primary expression, or by a method invocation expression E.Id(. . .), where E is a Primary expression, then the access is permitted if and only if the type of E is S or a subclass of S.

    So the protected member is accessible in all instances of S, and the answer in your book is just wrong.

提交回复
热议问题