Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

'protected' access control

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

While looking at the member access control modifiers in Java, I did some tests and found something rather unexpected when trying the 'protected' access modifier.

According to Oracle:

The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.



This is always true when accessing the protected member either from within the member's class, or from within a non-related class. When attempting to access a protected member from within a subclass, I found that I can access it as described above through either:
- the 'this' reference (implicitly or explicitly)
- the 'super' reference
- a reference variable of the same type as the subclass

However, when using a reference variable of another type (e.g., the member's class type or any of its other subclasses' types), I found that the description above doesn't hold. Instead, in these cases, the member is only accessible from within a class that's in the same package as the protected member's declaring class.

Can anyone explain why?

Thanks in advance!

Best regards,
Wouter
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of protected is for accessing members defined by our parent as our own members, but not letting them be accessible to the world. What you observed fits with that purpose.
 
author
Posts: 23908
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

Wout Er wrote:
This is always true when accessing the protected member either from within the member's class, or from within a non-related class. When attempting to access a protected member from within a subclass, I found that I can access it as described above through either:
- the 'this' reference (implicitly or explicitly)
- the 'super' reference
- a reference variable of the same type as the subclass

However, when using a reference variable of another type (e.g., the member's class type or any of its other subclasses' types), I found that the description above doesn't hold. Instead, in these cases, the member is only accessible from within a class that's in the same package as the protected member's declaring class.

Can anyone explain why?



See section 6.6.2 of the Java Language Specification...

Java Language Specification wrote: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.



Henry
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic