Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class modifiers

 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the first edition of Java 2 Exam Cram, chapter 4, pg 61.

"Classes cannot be protected, private, native, static, or sychronized. The words "abstract" and "final" cannot appear together because an abstract class, by definition, has declared methods that do not have implementations"
Doesn't this leave classes to be only public?
Why can't you have private classes? What if you don't want developers to use a class as its basically a helper class? (In that case you would probably have it as an inner class right but even in that case I may want to have it be private for encapsulation purposes...I think)
Any quick explanation for the class modifiers.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A top-level class can be public or it can have no modifier, in which case only other classes in the same package would have access to it.
If a top-level class was allowed to be private, who would be able to access it?
 
Matt Kidd
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ron Newman:
A top-level class can be public or it can have no modifier, in which case only other classes in the same package would have access to it.
If a top-level class was allowed to be private, who would be able to access it?

I want to say classes within the package but I have this inkling its wrong (private modifier would override the default protected modifier, if one were not given, thus nullifying the package view that no class modifier or the protected modifier gives.) but I can't put my finger on exactly why.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private means that it can only be accessed by ITSELF . If a top-level class were private then NOBODY could get at it.
If you have no access modifier then it is "friendly" or "package" access and only the classes in the same package can access it. This is a valid thing to do.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic