This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in protected and final classes( K & B book) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in protected and final classes( K & B book)" Watch "doubt in protected and final classes( K & B book)" New topic

doubt in protected and final classes( K & B book)

Radha Kamesh
Ranch Hand

Joined: May 19, 2007
Posts: 33
Hi friends,

This is a question from Self Test of Chapter 2: "Declarations and Access Control" of Kathy Sierra Book. I understand that a non nested class cannot be marked protected as rightly pointed out in this example. However when it is said...�Nonnested classes cannot be marked protected (or final for that matter),� what does it mean? Can�t outer classes be marked final?? Then why is it that I don�t get a compiler error when trying to do so? did I get the meaning of the sentence wrong ??? i'm confused !

8. Given the following,

1. import java.util.*;
2. public class NewTreeSet2 extends NewTreeSet {
3. public static void main(String [] args) {
4. NewTreeSet2 t = new NewTreeSet2();
5. t.count();
6. }
7. }
8. protected class NewTreeSet {
9. void count() {
10. for (int x = 0; x < 7; x++,x++ ) {
11. System.out.print(" " + x);
12. }
13. }
14. }

what is the result?
A. 0 2 4
B. 0 2 4 6
C. Compilation fails at line 4
D. Compilation fails at line 5
E. Compilation fails at line 8
F. Compilation fails at line 10


8. _ E. Nonnested classes cannot be marked protected (or final for that matter), so the
compiler will fail at line 8.
_ A, B, C, and D are incorrect because of the explanation given above.
[ August 20, 2007: Message edited by: Radhika ]
sandeep atluri
Ranch Hand

Joined: Apr 19, 2007
Posts: 42

did you remember the first thing about class declarations....

a top level class can only be marked as public or default.
since.. in your question...

the second class is not a nested class....
so it cannot be marked anything except public or default....

if you declare the class as public.. the compiler will say that the second class should be kept in a file named after its name(2nd class), not in the first class source file...

so the only choice you are left with is to keep the default modifier...

the 2nd question asked by you is..

whether it can be marked final or not...

now..let's see..if you had marked the NewTreeSet class as final....


you cannot subclass it...
i.e., you cannot make the NewTreeSet2 class, a subclass of NewTreeSet class.
so as per your code... you cannot declare NewTreeSet as Final...

besides.... whats the use of keeping a class when you cannot use it???)

got it....

Sandeep Atluri
Radha Kamesh
Ranch Hand

Joined: May 19, 2007
Posts: 33

Thanks for responding...

You are right. i cant mark newTree as final in this example as it wont make any sense to do so... but what i meant was...

in general ,when we say "non-nested classes cannot be marked final"...
doesn't it mean we can never mark any top-level class final???

i am confused because , we can definitely create a final class...( top level classes can be marked abstract,public,default,final,strictfp,) ...
i know it doesn't make sense to do so in this example but generally its possible right?

isn't it so??? then,why has the author said we cant,thats what i dont understand?

Radha Kamesh
Have you checked out Aspose?
subject: doubt in protected and final classes( K & B book)
It's not a secret anymore!