• Post Reply Bookmark Topic Watch Topic
  • New Topic

Protected access modifier at class level  RSS feed

 
Rajat Jindal
Ranch Hand
Posts: 44
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why "protected" can't be used as access modifier at class level whereas "default" can be used which is more restrictive than "protected" ? For example:-




 
Ashwin Soni
Greenhorn
Posts: 18
Android Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Top level classes cannot be protected because protected members are accessible only in same package in its and sub classes in other package. At top level these are meaningless.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you explain what it is supposed to mean if a top-level class would be 'protected'?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajat,

I broke up that enormous line in your code. Please don't put very long lines inside code tags; it screws up the windowing.
In fact, I suggest you give the UseCodeTags page another read.

Thanks

Winston
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ashwin Soni
Greenhorn
Posts: 18
Android Flex Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jiafan Zhou
Ranch Hand
Posts: 193
Fedora Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Top level class can either be declared as public or package. Thus protected top level class is not VALID.
 
B Verma
Greenhorn
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!