Re: protected confusion

Discussion in 'Java' started by Michael Rauscher, May 31, 2004.

  1. schrieb:
    > If I had not used any access modifier in ClassInPack1 I would have
    > package visibility of the members in
    > ClassInPack1 and I could understand why test.pack2.Class1Extender
    > would not compile
    > As it is I have protected access so I would have thought that I could
    > extend ClassInPack1 in any
    > package and have access to any protected member regardless of package.
    > I can get hold of the int but not the Inner class.
    > If I change the access modifier of ClassInPack1InnerClass to public
    > everything works fine.
    > Can anyone explain why this is ?


    This is, because it's specified to be :)

    From "The Java Language Specification" (where the term member includes
    at least class, interface, field and method):

    6.6.2 Details on protected Access
    A protected member or constructor of an object may be accessed from
    outside the package in which it is declared only by code that is
    responsible for the implementation of that object.

    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.

    Bye
    Michael
    Michael Rauscher, May 31, 2004
    #1
    1. Advertising

  2. Michael Rauscher

    Guest

    On Mon, 31 May 2004 20:43:50 +0200, Michael Rauscher
    <> wrote:

    > schrieb:
    >> If I had not used any access modifier in ClassInPack1 I would have
    >> package visibility of the members in
    >> ClassInPack1 and I could understand why test.pack2.Class1Extender
    >> would not compile
    >> As it is I have protected access so I would have thought that I could
    >> extend ClassInPack1 in any
    >> package and have access to any protected member regardless of package.
    >> I can get hold of the int but not the Inner class.
    >> If I change the access modifier of ClassInPack1InnerClass to public
    >> everything works fine.
    >> Can anyone explain why this is ?

    >
    >This is, because it's specified to be :)
    >
    > From "The Java Language Specification" (where the term member includes
    >at least class, interface, field and method):
    >
    >6.6.2 Details on protected Access
    >A protected member or constructor of an object may be accessed from
    >outside the package in which it is declared only by code that is
    >responsible for the implementation of that object.


    OK, I think I understand this now

    I can gain direct access to the protected int in the superclass in a
    subclass in another package because that subclass is responsible for
    implementing the superclass (when I construct the subclass I construct
    the superclass) However I don't have direct access to the protected
    class in the superclass because the subclass is not responsible for
    for implementing the protected class. I can subclass the protected
    class in my subclass thus.

    package test.pack2;

    import test.pack1.*;

    public class Class1Extender extends ClassInPack1{

    class X extends ClassInPack1InnerClass{
    public String getMessage(){
    return super.getMessage();
    }
    }
    ....

    and this works fine

    Anyway, thanks for taking the time to reply and I now have the java
    language spec in my online link library.

    Cheers
    Lyall
    , Jun 2, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    2
    Views:
    551
    Andreas Klemt
    Jul 5, 2003
  2. clintonG

    Re: What means Protected WithEvents ?

    clintonG, Jul 5, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    429
    clintonG
    Jul 5, 2003
  3. Jon
    Replies:
    4
    Views:
    5,631
    Jonathan Williams
    Aug 7, 2003
  4. Philippe Meunier
    Replies:
    1
    Views:
    295
    Mike Moore [MSFT]
    Oct 24, 2003
  5. Rob Richardson

    "protected" confusion

    Rob Richardson, Aug 1, 2007, in forum: C++
    Replies:
    2
    Views:
    287
    Rob Richardson
    Aug 1, 2007
Loading...

Share This Page