• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt regarding protected members access by sub-class

 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tony Docherty
Bartender
Pie
Posts: 2878
59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks henry. that was the only reason I knew about the question. now even more after your post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic