Two Laptop Bag*
The moose likes Beginning Java and the fly likes Protected Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Protected Class" Watch "Protected Class" New topic
Author

Protected Class

Gautam Sathe
Greenhorn

Joined: May 24, 2001
Posts: 18
Why we cannot use protected access specifier for a class?
------------------
NIKHIL JAIN
Greenhorn

Joined: May 29, 2001
Posts: 6
if u have noticed u can't even specify keyword PRIVATE before a class name.
giving a PRIVATE or PROTECTED specifier to a class name is of no meaning!!
its simple there r only two options, either u will be using ur class within the same package or outside in a different package. In the former case NO specifier is required and in the latter case, using PUBLIC specifier will work.
so whats the need for others!!!
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
The actual answer to this (very good question) is that the JVM has needs in addition to those of us poor programmers. One thing that the JVM does is to create a "ClassFile Structure" for each class that holds all the stuff that the JVM needs to know about the class. In this ClassFile Structure is an access_flags set of switches. One of those switches is dedicated to

ACC_PUBLIC 0x0001 = Declared public; may be accessed from outside its package.

Notice that this is a ONE BIT switch. It must be either ON or OFF. None of this "Well yeah any one in the package OR any subclass outside the package or . . . " stuff. Nope, it thinks in black and white. "Can anyone outside this package get at me - yes or no?

However - You don't really have a problem here.
Getting at a class is useless unless you can MAKE an instance of the class or USE the static stuff of the class. If you want to change the accessibility of a class, you change the access modifier on the constructor(s) and/or the access modifiers on the static variables and static methods themselves.
Therefore you always have the option of making the class public and restricting it's use. Doing it this way provides all the flexibility in restrictions that you might need.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You can use protected/private for "inner" classes. i.e. classes declared within another class.
Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
True, but then they are tracked as members of the Outer class, with all the accessiblity capabilities of other members.
Gautam Sathe
Greenhorn

Joined: May 24, 2001
Posts: 18
I am thankful to those who replied to my query, especially Cindy Glass for a nice clear reply.
Thanks,
Gautam.

------------------
 
 
subject: Protected Class