aspose file tools*
The moose likes Beginning Java and the fly likes Doubt regarding protected members access by sub-class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding protected members access by sub-class" Watch "Doubt regarding protected members access by sub-class" New topic
Author

Doubt regarding protected members access by sub-class

Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Hi

Why is it that protected members of a class in one package can only be accessed via inheritance by its sub-class in a different package? Why is super-class reference access not allowed by the compiler?


~ Mansukh
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2316
    
  49
Why is it that protected members of a class in one package can only be accessed via inheritance by its sub-class in a different package?

Not true, protected members can be accessed within their own package and by a subclass of its class in another package.

Why is super-class reference access not allowed by the compiler?

Not sure what you mean by this, can you explain in more detail.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Agreed on your correction. I forgot to add "classes within the same package". What I mean is that I cannot say :





Are you able to understand my question Tony?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

It's difficult to give a much better answer than "because that's what protected means". protected is there to allow classes to be extended in other packages. You don't need access by reference to be able to do that, so there's little need to allow it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Another topic that may be worth reviewing...

http://www.coderanch.com/t/598766/java/java/Access-Control-unable-access-protected

Pretty much covers the same issues.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

We would have to have a word with James, Joshua or someone to completely understand why protected behaves in this peculiar manner. Thanks anyways Henry.
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

A vague thing but I think that is actually the difference between how to use a protected members and public members in a subclass of different package.
A public members can be accessed with the (.) operator from anywhere.


OCPJP
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Ishan Pandya wrote:A vague thing but I think that is actually the difference between how to use a protected members and public members in a subclass of different package.
A public members can be accessed with the (.) operator from anywhere.


IMO, there is nothing vague about this... it is a clear and simple definition. It is somewhat compounded by stuff that makes it a little murky, but each item is also clearly defined in the Java Language Specification.

The definition is simple. A protected member is a member that is accessible by subclass implementations. And like the other access modifiers, it is at a class level (no worries about instances). A very simple definition.


As for the items that make it murky...

There is the default access level, which means accessible by the same package. This level is also sometimes called package private. This access level is between private and protected -- we can actually debate whether this is the correct position for it, but that's a side issue, it is where it is. Since the protected access level is less restrictive than the default access level, this means that protected members can be accessed by the classes in the same package of the class where the member is defined. In other words, the protected access level also allows the default access level.

The other item is the concept of "responsible for the implementation". This is needed because the access level is based on class, not instances. So, there needs to be a way to determine access when the implementation is using a reference to access another instance. The designers chosen the concept that a class can only access a protected member of a super class only if the instance is of type of its class or subclass (ie. responsible for the implementation). This is also a bit murkier since there is no way to determine the actual instance at compile time, so it just uses the reference type (and type checks at runtime).

Henry
Ishan Pandya
Ranch Hand

Joined: Feb 06, 2012
Posts: 223

thanks henry. that was the only reason I knew about the question. now even more after your post.
 
Consider Paul's rocket mass heater.
 
subject: Doubt regarding protected members access by sub-class