Sierra/Bates, page 136
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
Consider the following program:
import java.util.*;
public class NewTreeSet2 extends NewTreeSet {
public static void main(String [] args) {
NewTreeSet2 t = new NewTreeSet2();
t.count();
}
}
protected class NewTreeSet {
void count() {
for (int x = 0; x < 7; x++,x++ ) {
System.out.print(" " + x);
}
}
}
This program wont compile because
"Nonnested classes cannot be marked protected (or final for that matter), so the compiler will fail at line 8."
I dont understand this concept..can someone please explain?
Thanks,
Cathy
Consider the following program:
import java.util.*;
public class NewTreeSet2 extends NewTreeSet {
public static void main(String [] args) {
NewTreeSet2 t = new NewTreeSet2();
t.count();
}
}
protected class NewTreeSet {
void count() {
for (int x = 0; x < 7; x++,x++ ) {
System.out.print(" " + x);
}
}
}
This program wont compile because
"Nonnested classes cannot be marked protected (or final for that matter), so the compiler will fail at line 8."
I dont understand this concept..can someone please explain?
Thanks,
Cathy
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I dont understand this concept..can someone please explain?
Let's put it a little bit more understandable:
You cannot have a top level class declared as protected. Only public or default.
only inner classes can be declared protected.
Does that answer your question?
Let's put it a little bit more understandable:
You cannot have a top level class declared as protected. Only public or default.
only inner classes can be declared protected.
Does that answer your question?
I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Cathy Song
Ranch Hand
Posts: 270
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Andres,
I cant figure out why top level classes CANT be protected.
Thanks,
Cathy
I cant figure out why top level classes CANT be protected.
Thanks,
Cathy
posted 22 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
BTW, there's nothing wrong in declaring a top-level class final as long as you don't intend to extend it. 

Vad Fogel
Ranch Hand
Posts: 504
posted 22 years ago
JLS 7.6 Top Level Type Declarations
Hope, this helps.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
It is a compile-time error if a top level type declaration contains any one of the
following access modifiers: protected, private or static.
JLS 7.6 Top Level Type Declarations
Hope, this helps.
posted 22 years ago
When you choose an access modifier, you are actually thinking access across package boundary. If you look at the four access modifiers, coderanch, protected, default and private, the first 2 CAN cross package boundary, while the last 2 CANNOT.
Now, when it comes to top level class, the requirement is more specific to extension and instantiation. If you want your class to be extended or instantiated from within or outside the package, you use PUBLIC, otherwise, you use the default access. So what about protected and private? Well, they won't do anything different if they are allowed.
Supposing a protected modifier is allowed for a top-level class. If you extend it outside the package, how would it differ if you defined it public? Remember that members do not follow the modifier of the enclosing class, and the subclass does not follow the modifier of the superclass.
Just my 2 cents.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Cathy Song:
Andres,
I cant figure out why top level classes CANT be protected.
Thanks,
Cathy
When you choose an access modifier, you are actually thinking access across package boundary. If you look at the four access modifiers, coderanch, protected, default and private, the first 2 CAN cross package boundary, while the last 2 CANNOT.
Now, when it comes to top level class, the requirement is more specific to extension and instantiation. If you want your class to be extended or instantiated from within or outside the package, you use PUBLIC, otherwise, you use the default access. So what about protected and private? Well, they won't do anything different if they are allowed.
Supposing a protected modifier is allowed for a top-level class. If you extend it outside the package, how would it differ if you defined it public? Remember that members do not follow the modifier of the enclosing class, and the subclass does not follow the modifier of the superclass.
Just my 2 cents.
posted 22 years ago
The protected modifier is designed for strictly limited access for subclasses, ie for inheritance only. Or to put it another way, the subclass cannot use a reference to the superclass and access it using the dot operator. What the subclass can do, however, is to use a reference to its own class to inherit the protected member.
[ September 01, 2003: Message edited by: Roger Chung-Wee ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I cant figure out why top level classes CANT be protected.
The protected modifier is designed for strictly limited access for subclasses, ie for inheritance only. Or to put it another way, the subclass cannot use a reference to the superclass and access it using the dot operator. What the subclass can do, however, is to use a reference to its own class to inherit the protected member.
[ September 01, 2003: Message edited by: Roger Chung-Wee ]
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
| Please do not shoot the fish in this barrel. But you can shoot at this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |






