Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected Class

 
Gautam Sathe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we cannot use protected access specifier for a class?
------------------
 
NIKHIL JAIN
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use protected/private for "inner" classes. i.e. classes declared within another class.
Bosun
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, but then they are tracked as members of the Outer class, with all the accessiblity capabilities of other members.
 
Gautam Sathe
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am thankful to those who replied to my query, especially Cindy Glass for a nice clear reply.
Thanks,
Gautam.

------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic