• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access Modifiers and Classes

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quick Question:
What is the logic behind not being able to declare a class as protected. It can be public and can have default access, but not protected. It seems strange that this "middle-of-the-road" access modifier doesn't apply to classes.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Shane,
it makes more sense to have class as either Public or Default access specifier.
- if the class is public then it can be referred by any external classes and instantiated if there is a proper constructor rights (ie. constructor is not protected or private you know)
- if the class has default access specifier then it means it is accessible only from within the package.
by providing protected,private access specifier at more granular levels at methods/members makes more sense and provides flexibility. say, if we wanted to have all the methods as protected then we could think that "oh, it might be nice if Java allowed writing protected in front of the class itself as all the methods are protected.." but when we want to add one public method to that class we would end up doing the following,
1. remove protected from front of the class (as one method is public now)
2. add protected to each method except the one we wanted to be public
isn't this lot of work? this is an overhead , right?
instead just by having protected/private access specifiers at method level we can make class only accessible by package members if we have protected constructor, right?? and when want to add a public method to a public class then we just write that method and declare it as public rather than doing the excercise we mentioned in two steps above.
any more ideas??
regards
maulin.
 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yo maulin, could you elaborate on this bit of what you said please?
"instead just by having protected/private access specifiers at method level we can make class only accessible by package members if we have protected constructor, right?? "
thanks, i am just wondering what would happen if a class was default package access, but with a protected constructor?
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess if a class was public but had a protected constructor, the class could be accessed by anywhere, but could only be instantiated by class inside the package or by subclasses outside the package?
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Jasper,
you are right in your last post.
about the second last post of yours, my statement is confusing i agree. even its confusing to me now
but the question you asked "if what i create a class with default access specifier and the protected constructor?"
that would mean that the class can't be referred outside the package. we can't instantiate the class outside the package. even if u make constructor public instead of protected then also it won't work because we are not able to refer to the class in first place so there won't be question of invoking a constructor....
this link might be
more helpful regarding this.
this private/protected access specifier thing seems new everytime i think
regards
maulin.
 
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic