• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Old Chestnut: Protected Access

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think I understand that a member method/variable marked "protected" is accessible (through a reference) by classes in the same package, not accessible (through a reference) by classes in a different package, and is inherited by subclasses regardless of which package they belong to. (If I'm wrong please correct me)

I am confused about the level of access of that a "protected" member has once it has been inherited by a subclass.

Is it true that an inherited member, that was "protected" in its superclass, is "private" in the subclass, apart from the fact that it can be inherited by subclasses (of the subclass) regardless of which package they belong to?

Or does the subclass (of the subclass) have to be in the same package as the subclass to inherit the inherited "protected" member?

Also (and this is getting a bit confusing for me... well... a bit more confusing anyway) what access level does that member have once it has been inherited by a subclass of the subclass of the original class that had the "protected" member? Is it again the special "private"-but-can-be-inherited"?

Does this all mean that there are in fact 5 levels of access in Java?

Public
Protected
default (or package level)
"private-but-can-be-inherited" (an inherited protected member)
Private

Any help would be greatly appreciated.

Mikey
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Given the following class myClassA:

and myClassB


A compilation error will occur, because the protected modifier's
effects of myMethodA() propagates through it's child classes, unless
you override the method, like:



K&B indicates the visibility of protected members is:


Package and kids


So, there are only four access modifiers, from a given
class you have to trace back where the method has been
declared.

Cheers,

Gian Franco
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it's important to remember that a protected member variable is accessible to any subclasses of the class in which it is defined (regardless of the package the subclass is in) and the package in which it is defined.

That means that any class in the same package can access the variable, even if it's through a subclass within that package. This code prints 10:



Now, if that doesn't rattle your noodle, try this on for size:



So what do you think that does? Well, it prints 13, of course.

The member variable, a, is defined in package one. Therefore, the class OtherClassInOne, has access to it, even though it is accessing it via a class in another package, two.Sub.

So protected members are accessible in two situations:

1. By any class that extends the class in which it is defined.
2. By any class defined in the same package in which it is defined.

I can't think of a single situation that counters that. You can read more about proteceted access in the JLS, §6.6.2 Details on protected Access.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mikey Mike,

Welcome to Javaranch

We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Protected members do not become private on inheritance (I think C++ does something like that somewhere, but Java does not).

They remain protected.
 
Michal Charemza
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if a class, say classSub, extends a class, classSuper, which has a protected member, memberProtected, does memberProtected "act" as though it was defined in classSub with an access level of "protected"?

Thanks

Michal
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mikey Mike:
So if a class, say classSub, extends a class, classSuper, which has a protected member, memberProtected, does memberProtected "act" as though it was defined in classSub with an access level of "protected"?


No, it still "acts" as if it was defined where it was originally defined. Adding on to the example I used earlier, if we try to compile this:



Compiling this would result in a compiler error because a is inaccessable.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the inherited member never leaves the class it was defined in, again unlike some other languages where the new class contains all inherited members of the parent class.
So the exact same visibility it had in the parent still applies, including package access rules applicable to the parent class.

Treat inherited members as living in an instance of the parent class which sits invisibly behind the inherited class instance and you won't be far off from what's really happening.
 
Michal Charemza
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jeroen Wenting:
So the exact same visibility it had in the parent still applies, including package access rules applicable to the parent class.



Thank you! This is exactly what I was looking for.

Michal
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic