This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Kathy/Bert - great explanation of protected members Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Kathy/Bert - great explanation of protected members" Watch "Kathy/Bert - great explanation of protected members" New topic

Kathy/Bert - great explanation of protected members

leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
Let me say that I am not looking for brownie points here, but I want to give a pat on the back to Kathy and Bert, in their book, for a great explanation of protected members. It was very thorough and I'm left with no questions to ask. This is rare.
Great job!

Thanks, leo
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572

(OK, no brownie points, but the check is in the mail )
cheers and good luck,
Kathy (and Bert, too)
preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
Can you just give me a summary of what was explained there about protected members...
1. protected members can be accessed in subclasses in another package only via references of their type or sub type...
2. If u try to access a protected member in a subclass in a nother package, via a reference of supertype it will result in error
Can somebody explain why the 2nd results in error...Is it because when protected members are accessed outside of the package via the superclass they behave more like private members... I remember marlene saying something similar in a big thread
thank you
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
You have the right idea -- 'protected' is different from the other three access levels because access to a protected member is ONLY through inheritance. In other words, with 'protected' a subclass gets access to the superclass protected member only by 'inheriting' the member -- but the subclass (outside the package of the superclass) does NOT get any special ability to access the protected member in any other way except for inheritance.
So, a subclass outside the package does not get to make an instance of the superclass, and then use that superclass reference to access the protected member. If you think of 'protected == inheritance', that will help you remember that inheritance is the only way in which protected is different from default. Protected is exactly the same as default for classes inside the same package.
But outside the package... protected is a special way for a superclass to say, "I want my children to inherit this method, that's the only way it can be used once my children move away (i.e. leave the package)"
Then the question is... so what happens to the protected member when it is inherited by a subclass outside the package? And you have 90% of the answer when you say that it behaves as though it were private. It does, almost. For example, if class Mini is a subclass of class Car, but Mini is the the "coolcars" package while Car is in a "vehicles" package, class Mini inherits the protected go() method of class Car. That is the only way Mini gets to use the method... by simply HAVING the method through inheritance. Mini HAS the method, but it cannot INVOKE the method on any other object (except other Minis, of course).
But now what about the other classes in the new "coolcars" package? Do they get to access the method through a reference to Mini? No. Once that method is inherited *outside* the package in which it was declared, nobody else in the Mini's package can access it. It acts 'private'. The only way to invoke it is to call some other method on Mini that Mini exposes to the outside world, and then that method in turn calls another Mini method -- the protected method that Mini inherited.
Hmmmm.... that's *almost* right. There is an exception here -- which is if there is a new subclass of Mini! Other subclasses of Mini still get access to the protected member that Mini inherited, but ONLY by also inheriting the protected member.
So, bottom line: once outside the package, a protected member can be accessed ONLY through inheritance. A subclass outside the package will INHERIT the protected member, but that is the only way the member can ever be accessed by any class outside the package. The inheritance of the protected member extends down the inheritance tree to subclasses of subclasses, but to any other class outside the original package of the superclass that declared the protected member, that member is essentially private.
Protected == inheritance, for any class outside the package of the class with the protected member.
For classes INSIDE the package of the class declaring the protected member, protected == default.
So, the final result is:
Protected is the same as default for all classes inside the package declaring the protected member.
Protected is the same as private for all classes outside the package of the class declaring the protected member, EXCEPT for all subclasses. And to those subclasses, they can access the member ONLY through inheritance.
preeti khane
Ranch Hand

Joined: Mar 12, 2003
Posts: 93
kathy, thanks
also protected acts as private when it comes to aggregation of classes in 2 different packages... which I understand with the above explanation
subject: Kathy/Bert - great explanation of protected members