Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struggling with explanation in book for OCA

 
Antonio Michele
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Hopefully this is the right forum for my concern.
I'm reading the book "A Programmer's Guide to Java SE 8 Oracle Certified Associate by Khalid A Mughal and Rolf W Rasmussen" but I'm struggling with
an explanation about protected member and how they can not be accessed from an superclass object in another package, only from subclasses or subsubclasses.
(Example 4.10 illustrates the point in the book)
The explanation says:
„The class SubclassB declares the field objRefA of type SuperclassA at (1). The method subclassMethodB() has the formal parameter objRefB of type SubclassB. Access is permitted to a protected member of SuperclassA in packageA by a reference of the subclass, as shown at (2) and (3), but not by a reference of its superclass, as shown at (4) and (5). Access to the field superclassVarA and the call to the method superclassMethodA() occur in SubclassB. These members are declared in SuperclassA. SubclassB is not involved in the implementation of SuperclassA, which is the type of the reference objRefA. Hence, access to protected members at (4) and (5) is not permitted as these are not members of an object that can be guaranteed to be implemented by the code accessing them.
Accessibility to protected members of the superclass would also be permitted via any reference whose type is a subclass of SubclassB. The previously mentioned restriction helps to ensure that subclasses in packages different from their superclass can access protected members of the superclass only in their part of the implementation inheritance hierarchy. In other words, a protected member of a superclass is accessible in a subclass that is in another package only if the member is inherited by an object of the subclass (or by an object of a subclass of this subclass)

My problem is the bold part. I'm no native speaker and I would be grateful if somebody could explain what the authors meant with this sentence.

Best regards
Proton
code.png
[Thumbnail for code.png]
Example Code
 
Henry Wong
author
Marshal
Pie
Posts: 22104
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The author is referring to the "responsible for implementation" rule, related to accessing protected fields.

https://docs.oracle.com/javase/specs/jls/se8/html/jls-6.html#jls-6.6.2

For more information, just search for topics regarding this rule on the ranch:

https://coderanch.com/t/665302/java/Protected-access-code-responsible-implementation
https://coderanch.com/t/657359/java/protected-constructor
https://coderanch.com/t/238030/certification/Protected-members-inheritance

Henry

 
Antonio Michele
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I read every thread a few times and I think I have a clue now.
Will sleep over it and hopefully understand it tomorrow. ;)

Thanks a lot!

Best regards
Proton
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic