aspose file tools*
The moose likes Beginning Java and the fly likes Access Modifiers and Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Access Modifiers and Classes" Watch "Access Modifiers and Classes" New topic
Author

Access Modifiers and Classes

Shane Petrie
Greenhorn

Joined: Apr 15, 2002
Posts: 2
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.
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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.
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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?


giddee up
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
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

Joined: Nov 04, 2001
Posts: 1873
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access Modifiers and Classes