If we make a class as "protected", I know it's not a good programming practice to have a class as "protected" but I don't understand why it is not allowed. Since "protected" lies between "public" and "default" access specifier. What's made Sun to remove "protected" from class level access specifier. Please give me some relevant information. Thanks in Advance.
Please don't mention answers like what's the use of having "protected" as a class level access specifier.
Rajat Jindal wrote:Please don't mention answers like what's the use of having "protected" as a class level access specifier.
Why not? Most answers to the question "why isn't this keyword allowed here?" are "because it wouldn't make any sense". The best way to approach these questions is to try and work out what it would mean if it was allowed, and often that provides the answer.
So, if protected was allowed here, what would you expect it to mean? Here's what I'd expect. I'd expect it to be the same as default, except the class would be visible to subclasses. But how do you subclass something that isn't visible already?
Jesper de Jong wrote:Can you explain what it is supposed to mean if a top-level class would be 'protected'?
Suppose the top level class is protected in that case we have the advantage of default(friendly) behavior and plus access to sub classes in another package but in that case there will be a problem that for the subclass of another package that top level class will we like private . Basically outer sub classes cannot access the PROTECTED top level class therefore it become meaningless.
Matthew Brown wrote:I'd expect it to be the same as default, except the class would be visible to subclasses. But how do you subclass something that isn't visible already?
Its a perfect answer to "Why can't we have the 'protected' modifier for a top-level class". Assume it's allowed to use protected modifier for a class. Then what will happen, it will be visible to all the classes in the same package which is the same behavior what a default (package-level) access class will possess. Additionally this 'protected' class should be visible to all the subclasses outside package also. But unfortunately you would not be able to create any subclass of this class outside the package because this class itself will not be visible outside the package. Hence without the subclass specific behavior, this 'protected' class will be exactly same as a package-level or default access class. So, there is absolutely no need of 'protected' modifier for classes and hence, not permissible as well.
Hope, it helps although I just tried to elaborate Matthew's point.
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad: