why cant a class have its modifiers as private or protected?

Hi all,I wanted to know why cant a class have its modifiers as private or protected i mean to say why cant we write a class asprivate class A{}plz answer and thx in advance.
[215 byte] By [sagar123a] at [2007-11-26 18:08:44]
# 1
u cant access that class outside the class.And main class is needed to be called outside ur program by JVM
rakesh_thakura at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 2
How would you use a class that can only be accessed from within itself?For protected, it wouldn't make any sense. Why would you want a class that could only be accessed from the same package and by its child class?
jverda at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 3
but suppose i write the main method in some other class which is public then also why cant i write a class which has its modifier as private or protected.
sagar123a at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 4
It seems like you are talking about inner classes. You don't need to mark them as private or protected since they are available only within the class itself.
Ilana at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 5
I agree that using private modifier doesnt make sense but why cant we use protected modifier for a class
sagar123a at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 6
What is the meaning of a protected class?I've seen methods being protected when you override them, but what is a protected class?An inner class is limited in scope to its parents class. Isn't this what you are trying to do?
Ilana at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 7
> but suppose i write the main method in some other> class which is public then also why cant i write a> class which has its modifier as private or protected.How could you ever use that class?
jverda at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 8

> What is the meaning of a protected class?

> I've seen methods being protected when you override

> them, but what is a protected class?

> An inner class is limited in scope to its parents

> class. Isn't this what you are trying to do?

You mean a nested class? No. The enclosing class is not usually its parent, and a nested class can have public visibility.

If you mean an anonyous inner class, public/protected/etc. don't apply because you can never refer to it by name anyway.

jverda at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 9

I'm still new enough that I'm not sure what the difference is between a nested class and an inner class. Anonymous inner classes are not what I'm referring to. What I do have are nested or inner classes which are extentions of SwingWorker.

These are defined within the parent and appear as myClass$myName.class, whereas anonymous classes appear as myClass$1.class.

Ilana at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 10

> I'm still new enough that I'm not sure what the

> difference is between a nested class and an inner

> class. Anonymous inner classes are not what I'm

> referring to.

Inner classes are a certain kind of nested class--ones that are non-static, I think.

> What I do have are nested or inner

> classes which are extentions of SwingWorker.

> These are defined within the parent and appear as

> myClass$myName.class,

So, inside some class of yours, you define public class MyClass extends SwingWorker? If so, those classes are accessible outside the class that defines them.

jverda at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 11
I do NOT use the word public.It is defined as class MyClass extends SwingWorker and I have no reason to want those classes outside the parent.
Ilana at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 12

> I do NOT use the word public.

> It is defined as class MyClass extends SwingWorker

> and I have no reason to want those classes outside

> the parent.

Ah, then it's package-private and is only visible within the package. And you mean outside the enclosing class, not outside the parent. SwingWorker is the parent, not the class inside which MyClass is defined.

I thought you said earlier that no nested class could be accessed from outside its enclosing class.

jverda at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 13

jverd,

> For protected, it wouldn't make any sense. Why would you want a class that

> could only be accessed from the same package and by its child class?

Ummmm... isn't that exactly what you get if you omit the public modifier on the class definition? And doesn't that allow you to organise the implementation details within a package as you see fit, without effecting the public interface. And isn't that useful?

Or am I missing something really basic? Or (Great Scott!!!) Am I just plain wrong?

keith.

corlettka at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 14

> jverd,

>

> > For protected, it wouldn't make any sense. Why

> would you want a class that

> > could only be accessed from the same package and by

> its child class?

>

> Ummmm... isn't that exactly what you get if you omit

> the public modifier on the class definition?

Nope. That's package only, not child classes.

> And

> doesn't that allow you to organise the implementation

> details within a package as you see fit, without

> effecting the public interface. And isn't that

> useful?

The package-private as it is (package only, not subclasses) does, yes.

jverda at 2007-7-9 5:40:29 > top of Java-index,Java Essentials,New To Java...
# 15
jverd, thanx. my OOness is now in perfect alignment with Venus.
corlettka at 2007-7-21 17:16:06 > top of Java-index,Java Essentials,New To Java...