• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why protected modifier is not allowed?

 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code, there is an error on line 11 saying "modifier protected not allowed here". what is the reason?
 
Krisztian Milesz
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use the protected access modifier for top level classes.
The protected modifier means that the member can only be accessed within its own package or by a subclass of its class in another package.

At top level you can only use the public modifier or no modifier (package-private).

 
rakeshdec kumar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only modifiers that applicable for Outer classes are public

<default>

abstract

final

strictfp
 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you make a subclass protected if it is extending a public class? I think no, because it is restricting the access
Then when exactly can we use protected?
 
Stephan van Hulst
Bartender
Posts: 6311
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can never make a top level class protected. It makes no sense, it doesn't mean anything.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic