• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt regarding protected members access by sub-class

 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another topic that may be worth reviewing...

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

Pretty much covers the same issues.

Henry
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 228
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks henry. that was the only reason I knew about the question. now even more after your post.
 
reply
    Bookmark Topic Watch Topic
  • New Topic