Nested Classes, Part 1
Subject:   Accessing Protected Members
Date:   2004-05-18 12:33:43
From:   bdell
I changed resolution from private to protected access and ColorMonitorScreen compiled just fine, so I don't think the statement, "Since ColorPixelPoint is not a member of BasicMonitorScreen, access to BasicMonitorScreen's private and protected members is not allowed," is entirely correct.

I only tried it with 1.5beta, so maybe it's a language change, but I think it makes sense. A member of a sub-class should have access to protected members of the super-class.

Full Threads Newest First

Showing messages 1 through 3 of 3.

  • Accessing Protected Members
    2004-05-18 14:33:35  Robert Simmons, Jr. | O'Reilly Author [View]

    That is because protected members of a class are accessible to classes in the same package. I suppose I should have said that the other class needs to be in another package to be more clear.

    I appreciate the feedback.
    • Accessing Protected Members
      2004-05-18 21:28:10  bdell [View]

      I placed BasicMonitorScreen in package package1 and ColorMonitorScreen in package package2 and it still compiles. I also tried 1.4.2 and it works there as well. Perhaps I am misunderstanding what the "other" class is.

      Given the way inner classes are implemented, it seems natural to provide access as the accessor method the inner class depends on would have access to protected members of the enclosing class's super-class and, as I wrote before, it seems consistent with the definition of protected access. I couldn't find anything in the Java Language Specification that said protected members in this circumstance shouldn't be accessible.
      • Accessing Protected Members
        2004-05-21 15:42:38  kristianmonsen [View]

        I understand it so that if the enclosing class can access the variable, than the inner class can as well. Since the enclosing class is a subclass of the class where the variable is declared, it will always be able to access a protected variable.