File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why protected modifier is not allowed?

 
Anjali Vaidya
Ranch Hand
Posts: 40
Chrome Java Netbeans IDE
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic