As many others have noted, the limitation here has nothing to do with the fact that your class is abstract, but rather the visibility modifier that you have chosen to use. Keep in mind that each of these visibility modifiers means:
For the class in which you use the keyword ...
private: Only for this class
(default / package closed): Only visible to this class and classes in the package
protected: Visible for this class, classes in its package and subclasses of this class
public: Visible to any class
Top-level classes cannot be declared private , because then nothing can access them.
But your question is related to why they cannot be declared protected . It is clear that a protected top-level class (if it could exist) would be visible to itself (each class is visible on its own). It is also clear that the protected top-level class will be visible to classes in its package. However, making it visible to your subclasses is difficult. Which classes should be allowed to inherit our protected class?
If thatβs all, then our class can also be public , because then we say that any class has the right to access our protected class. If this is not one of them, then our class can also be package-private , since only two other conditions are fulfilled (visible to oneself and things in its package). And this cannot be βsome of them,β because we need a way to determine which classes can access it, which was primarily a visibility modifier.
Thus, for these reasons, top-level classes cannot be private or protected ; they must be with the package β private or public .
source share