• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....
Radha Kamesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
this is supposed to be a surprise, but it smells like a tiny ad:
the value of filler advertising in 2021
    Bookmark Topic Watch Topic
  • New Topic