• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

protected access

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everyone knows what the protected access modifier is with respect to the four types of "access modifiers" for instance variables (member fields).

private < default < protected < public

I am going to list the steps for accessing a protected field in a class that has been imported.

Steps involved to access a "protected" member field in a class of a separate package.


1. Import the package of the class containing the protected member.
2. The class you are using to access the protected member must extend the class containing the protected member (See // line a) below)
3. Access the protected member of the imported class by creating a reference variable of the subclass (See // lines 1 and 4) below)





For the code below, the class Human imports the package species.

Variable 'y' in class Mammal is protected [protected int y = 2;]
1) You can access the variable y in class Mammal if you create a reference variable of type Human as on line 1
line 2: variable 'x' in Mammal.java is public
line 3: you cannot access protected member field using a reference variable of the parent class (Class that contains the protected member field)
line 4: you can access the protected member field by creating a reference variable of the subclass (i.e. Human)
line 5: member field 'z' in class Mammal has default access
line 6: member field 'z' with default access cannot be accessed




The key point to note here is:
protected field y in class Mammal can only be accessed by creating a reference variable of the subclass Human.


If you attempt to access the protected member through a reference of Mammal (the class that contains the protected member)
it will result in a compiler error.

I consider this the implementation of the "protected access" concept .


Question: Is there anything else one should know about "protected access "?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:I am going to list the steps for accessing a protected field in a class that has been imported.

I don't like this phrasing. There is no such thing in Java as "importing a class" (as in some kind of copy & paste the contents of one class in another one). An import statement just allows you to use the class name (instead of it's fully qualified name), so you'll be saving lots of key strokes and your code has less clutter which makes it easier to read.

Thomas Hauck wrote:Steps involved to access a "protected" member field in a class of a separate package.

This is much better than the previous statement! It's really spot-on

Thomas Hauck wrote:Question: Is there anything else one should know about "protected access "?

Understanding the bits and pieces of "protected" access (and the subtle different with "default" access) is one of the hardest parts of the OCAJP7 certification. As a reminder think of default = package-private and protected = package-private + kids. If the subclass is in a different package, then you can only access a protected member from its superclass by inheritance (not using a reference variable).

To illustrate I updated your example a little bit. The Mammal class isn't changed. I also included unique line indicators, so you can use them to ask questions if needed.





Hope it helps!
 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:
The key point to note here is:
protected field y in class Mammal can only be accessed by creating a reference variable of the subclass Human.


If you attempt to access the protected member through a reference of Mammal (the class that contains the protected member)
it will result in a compiler error.


The relevant section of the JLS is section 6.6.2...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-6.html#jls-6.6.2

And the exact wording is that it can be accessed "by code that is responsible for the implementation of that object". So, for the code in the Human class to access the protect field of the Mammal class, via a reference, it needs the reference to be to an Object that IS-A Human class. This means that it needs to be accessed via a Human object, or a subclass of the Human object.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic