January 29, 2005

no that protected, it seems

During a conversation with Diego today, he pointed out that the protected qualifier in Java does not prevent access to classes within the same package.

That was news to me. I always thought protected limited the access to subclasses of the class that contains the protected member.

As Diego points out in his weblog, it seems the Java Language Specification is incomplete. The ambiguity is actually cleared up in section 6.6.1 of the spec:
  • A member (class, interface, field, or method) of a reference (class, interface, or array) type or a constructor of a class type is accessible only if the type is accessible and the member or constructor is declared to permit access:
    • If the member or constructor is declared public, then access is permitted. All members of interfaces are implicitly public.
    • Otherwise, if the member or constructor is declared protected, then access is permitted only when one of the following is true:
      • Access to the member or constructor occurs from within the package containing the class in which the protected member or constructor is declared.
      • Access is correct as described in ยง6.6.2.

0 Comments:

Post a Comment

<< Home