• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Protected Class

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why we cannot use protected access specifier for a class?
------------------
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!!
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

------------------
 
They weren't very bright, but they were very, very big. Ad contrast:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic