• 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 ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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


Answer

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
hey...

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....

then....

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
hi,

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
 
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic