Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Re: protected confusion

Reply
Thread Tools

Re: protected confusion

 
 
Michael Rauscher
Guest
Posts: n/a
 
      05-31-2004
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

 
Reply With Quote
 
 
 
 
lyallex@nospam.yahoo.co.uk
Guest
Posts: n/a
 
      06-02-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"protected" confusion Rob Richardson C++ 2 08-01-2007 02:50 PM
Flash drive is write protected!! =?Utf-8?B?a2E=?= Wireless Networking 0 10-14-2005 04:12 AM
automating logon to passwd protected urls ? ngoc Firefox 0 09-29-2005 02:18 PM
"Make sure disk is not full or write-protected" error???? Ric Wireless Networking 4 09-16-2005 10:42 AM
Difference between "Protected WithEvents myClassName" And "Protected myClassName" ? Andreas Klemt ASP .Net 2 07-05-2003 12:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57