• 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

Does 'protected' offer wider access than 'package' level?

 
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:
tb864615.OCPJAVASE17PT.c03.099

I was surprised to find this compiles.
Since the derived class does not access members in the package outside the base class, I thought that package has wider access than protected.
However since this code compiles, it means 'protected' offers wider access than 'package' level?

 
Bartender
Posts: 3915
43
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Your assumption is correct --  the protected access modifier is less restrictive than the package (default) access level
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Mikalai Zaikin wrote:Your assumption is correct --  the protected access modifier is less restrictive than the package (default) access level



I don't understand why it compiled.
As I understand, the implementing method should have wider access.
But here, the implementing method is protected (narrower) but the abstract method is package level (wider).
 
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:As I understand, the implementing method should have wider access.


Wider, meaning less restrictive, yes.  Or equally restrictive.

Anil Philip wrote:But here, the implementing method is protected (narrower) but the abstract method is package level (wider).


But protected is less restrictive than package, meaning it is wider, not narrower, than package.
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Mike Simmons wrote:
But protected is less restrictive than package, meaning it is wider, not narrower, than package.


I don't understand.
Since the derived class does not access members in the package outside the base class, I thought that package has wider access than protected.
 
Saloon Keeper
Posts: 15608
366
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:the derived class does not access members in the package outside the base class


package has wider access than protected


I see absolutely no logical connection between these two statements.

How would the first statement imply the second?
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
From a derived class, you can access:

Any protected or public member of a super class in another package.
Any public, protected, or package-access members of a class in the same package.
Any members, even private, of the same top-level class.

The second of these conflicts a bit with your statement:

Anil Philip wrote:the derived class does not access members in the package outside the base class


A derived class can access members of the same package as the derived class, outside any base class.  Try it - it works!
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Stephan van Hulst wrote:

Anil Philip wrote:the derived class does not access members in the package outside the base class


package has wider access than protected


I see absolutely no logical connection between these two statements.

How would the first statement imply the second?



A garage door has (or allows) wider access than the door to a house.
Since 'protected' does not allow access to the other members of the package of the base class (from the derived class),
protected is more restrictive.
Hence I thought that package is wider than protected.
 
Stephan van Hulst
Saloon Keeper
Posts: 15608
366
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:'protected' does not allow access to the other members of the package of the base class (from the derived class)


And how did you arrive at this statement?
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Stephan van Hulst wrote:

Anil Philip wrote:'protected' does not allow access to the other members of the package of the base class (from the derived class)


And how did you arrive at this statement?



I am trying to write a program to test this. Unsuccessful so far...

Diet is in Package Wolf and is not accessible from the derived class. Trying to code it with something that is protected.







 
Mikalai Zaikin
Bartender
Posts: 3915
43
  • Number of slices to send:
    Optional 'thank-you' note:
I wonder why you assumed the Diet can be accessed outside its package?

Please check JLS 6.6.1: https://docs.oracle.com/javase/specs/jls/se17/html/jls-6.html

If a top level class or interface is declared with package access, then it may be accessed only from within the package in which it is declared.

 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Mikalai Zaikin wrote:I wonder why you assumed the Diet can be accessed outside its package?

Please check JLS 6.6.1: https://docs.oracle.com/javase/specs/jls/se17/html/jls-6.html

If a top level class or interface is declared with package access, then it may be accessed only from within the package in which it is declared.



I am trying to understand which has wider access; package or protected.
I don't know the answer.
I want to see it in a program like this.
 
Stephan van Hulst
Saloon Keeper
Posts: 15608
366
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
A simple example. Here's a base class in package a:

Here's a class that doesn't derive from the base class, but is in the same package a:

Finally, here is a class in a different package b, that derives from the base class.

As you can see, you can access protected members from a class within the same package (even when the class doesn't derive from the base class); but you can't access package private members from a class in a different package that does derive from the base class.

Therefore, protected access is less restrictive than package private access. You could say protected access is 'wider' than package private access, but the preferred phrasing is "protected members have greater visibility than package private members".
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Stephan van Hulst wrote: but the preferred phrasing is "protected members have greater visibility than package private members".



So what you are saying is; protected members can be accessed from the same package and also from the derived class. But package-private members can only access each other within the same package.
Thank you. I had been struggling to understand. Your sentence above was key to my getting it finally. "protected members have greater visibility than package private members"
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • Number of slices to send:
    Optional 'thank-you' note:
If only it had been stated previously...
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Mike Simmons wrote:If only it had been stated previously...



I agree about 'protected' having greater visibility, but I don't see why one can say that 'protected' has wider access.
In the derived class DerivedService, it has lost access to the package members of com.example.a
In Stefan's example, if from the body of com.example.b.Service.protectedMethod(), (just suppose) it could access not only everything in DerivedService and also com.example.b (as expected),
but also everything in the original package com.example.a - then it has not only kept access that base class Service had, but also gained additional access to com.example.b and then one can say that 'protected' has wider access.
Perhaps it is a misnomer?

 
Marshal
Posts: 28258
95
  • Number of slices to send:
    Optional 'thank-you' note:
I agree. We really don't need a long thread on the meaning of the word "wider". It's really more useful to learn how the modifiers work in practice.
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:

Paul Clapham wrote:I agree. We really don't need a long thread on the meaning of the word "wider". It's really more useful to learn how the modifiers work in practice.



Some of the questions on the practice tests ask you which modifier provides 'wider' (or sometimes the word 'more') access.
 
Stephan van Hulst
Saloon Keeper
Posts: 15608
366
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:
Well, now you know. By wider, they mean higher visibility, and protected members are more visible than package private members.
 
Mike Simmons
Master Rancher
Posts: 4905
74
  • 1
  • Number of slices to send:
    Optional 'thank-you' note:

Anil Philip wrote:I agree about 'protected' having greater visibility, but I don't see why one can say that 'protected' has wider access.
In the derived class DerivedService, it has lost access to the package members of com.example.a


I don't think it ever had access, so I wouldn't say it lost anything.

Anil Philip wrote:In Stefan's example, if from the body of com.example.b.Service.protectedMethod(), (just suppose) it could access not only everything in DerivedService and also com.example.b (as expected),
but also everything in the original package com.example.a - then it has not only kept access that base class Service had, but also gained additional access to com.example.b and then one can say that 'protected' has wider access.
Perhaps it is a misnomer?


So, there is no com.example.b.Service.protectedMethod().  I guess you mean, if we put one there, and if we were calling other stuff from that method.  But that's beside the point.  It sounds like you're thinking that if a method is protected, it might confer more power to access stuff outside that method.  That's backward.  A modifier like "protected" on a method does not affect what that method can see or access.  Rather, it affect whether outside code can see or access that method.  That's why Stefan's code is testing what can be seen or accessed from other locations, namely com.example.a.SamePackage and com.example.b.DerivedService, which are testing their ability to access com.example.a.Service.protectedMethod() and access com.example.a.Service.packagePrivateMethod().
 
Anil Philip
Ranch Foreman
Posts: 626
2
  • Number of slices to send:
    Optional 'thank-you' note:
Thanks - I understood now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic
vceplus-200-125    | boson-200-125    | training-cissp    | actualtests-cissp    | techexams-cissp    | gratisexams-300-075    | pearsonitcertification-210-260    | examsboost-210-260    | examsforall-210-260    | dumps4free-210-260    | reddit-210-260    | cisexams-352-001    | itexamfox-352-001    | passguaranteed-352-001    | passeasily-352-001    | freeccnastudyguide-200-120    | gocertify-200-120    | passcerty-200-120    | certifyguide-70-980    | dumpscollection-70-980    | examcollection-70-534    | cbtnuggets-210-065    | examfiles-400-051    | passitdump-400-051    | pearsonitcertification-70-462    | anderseide-70-347    | thomas-70-533    | research-1V0-605    | topix-102-400    | certdepot-EX200    | pearsonit-640-916    | itproguru-70-533    | reddit-100-105    | channel9-70-346    | anderseide-70-346    | theiia-IIA-CIA-PART3    | certificationHP-hp0-s41    | pearsonitcertification-640-916    | anderMicrosoft-70-534    | cathMicrosoft-70-462    | examcollection-cca-500    | techexams-gcih    | mslearn-70-346    | measureup-70-486    | pass4sure-hp0-s41    | iiba-640-916    | itsecurity-sscp    | cbtnuggets-300-320    | blogged-70-486    | pass4sure-IIA-CIA-PART1    | cbtnuggets-100-101    | developerhandbook-70-486    | lpicisco-101    | mylearn-1V0-605    | tomsitpro-cism    | gnosis-101    | channel9Mic-70-534    | ipass-IIA-CIA-PART1    | forcerts-70-417    | tests-sy0-401    | ipasstheciaexam-IIA-CIA-PART3    | mostcisco-300-135    | buildazure-70-533    | cloudera-cca-500    | pdf4cert-2v0-621    | f5cisco-101    | gocertify-1z0-062    | quora-640-916    | micrcosoft-70-480    | brain2pass-70-417    | examcompass-sy0-401    | global-EX200    | iassc-ICGB    | vceplus-300-115    | quizlet-810-403    | cbtnuggets-70-697    | educationOracle-1Z0-434    | channel9-70-534    | officialcerts-400-051    | examsboost-IIA-CIA-PART1    | networktut-300-135    | teststarter-300-206    | pluralsight-70-486    | coding-70-486    | freeccna-100-101    | digitaltut-300-101    | iiba-CBAP    | virtuallymikebrown-640-916    | isaca-cism    | whizlabs-pmp    | techexams-70-980    | ciscopress-300-115    | techtarget-cism    | pearsonitcertification-300-070    | testking-2v0-621    | isacaNew-cism    | simplilearn-pmi-rmp    | simplilearn-pmp    | educationOracle-1z0-809    | education-1z0-809    | teachertube-1Z0-434    | villanovau-CBAP    | quora-300-206    | certifyguide-300-208    | cbtnuggets-100-105    | flydumps-70-417    | gratisexams-1V0-605    | ituonline-1z0-062    | techexams-cas-002    | simplilearn-70-534    | pluralsight-70-697    | theiia-IIA-CIA-PART1    | itexamtips-400-051    | pearsonitcertification-EX200    | pluralsight-70-480    | learn-hp0-s42    | giac-gpen    | mindhub-102-400    | coursesmsu-CBAP    | examsforall-2v0-621    | developerhandbook-70-487    | root-EX200    | coderanch-1z0-809    | getfreedumps-1z0-062    | comptia-cas-002    | quora-1z0-809    | boson-300-135    | killtest-2v0-621    | learncia-IIA-CIA-PART3    | computer-gcih    | universitycloudera-cca-500    | itexamrun-70-410    | certificationHPv2-hp0-s41    | certskills-100-105    | skipitnow-70-417    | gocertify-sy0-401    | prep4sure-70-417    | simplilearn-cisa    |
http://www.pmsas.pr.gov.br/wp-content/    | http://www.pmsas.pr.gov.br/wp-content/    |