This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Protected access modifier  RSS feed

 
shweta patiljadhav
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

derived class can inherit and access protected members of its base class, regardless
of the package in which it’s defined. A derived class in a separate package
can’t access protected members of its base class using reference variables.

and protected members are not accessible to unrelated classes in different package.

Please explain me first line.
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I will let other ranchers deal with the first line... as for the second line...

shweta patiljadhav wrote:
derived class can inherit and access protected members of its base class, regardless
of the package in which it’s defined. A derived class in a separate package
can’t access protected members of its base class using reference variables.



This is *not* true. A derived class in a separate package can access protected members of its base class using reference variables ... provided that it follows the "responsible for implementation" rule. This means that it can use reference variables, but it has to be the right type of reference variables.

In other words, the reference variable used by the derived class has to be IS-A derived class type.

Henry
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's subclass and superclass, not derived and base. Those terms are usually used in C#, so bplese be sure you have found a Java® reference. Also always tell us where such quotes come from.

Please explain the second sentence, which looks confused to me.
 
shweta patiljadhav
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to
Mala Gupta
book

if there are two packages, building and library,
book is base class in package library,



Protected members of class Book are not
accessible in derived class StoryBook, if
accessed using a new object of class Book
 
shweta patiljadhav
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
“How can you
declare a class Curtain in a package building so that it isn’t visible outside the
package building?”

and answer is because top level class can not be protected and private? being OCA candidate please explain me what is top-level class AND why it can't be protected if it can be default? I can understand it can't be private.

TIA
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java® Language Specification = JLS defines top level type declarations, though the JLS can be difficult to understand.
Try explaining what private would mean in the following declaration:-
/* not nested class */ private class Foo ...
When you have worked out why that combination is prohibited, you should have no difficulty working out why protected would not be allowed there either.
The definition of protected access is in the JLS:-
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.
If you have a Book class and a StoryBook class then the StoryBook class is responsible for creation of the Book object.
Have you run that sort of code? Did you suffer any compile time errors?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!