1. I know that clases cannot be private or protected. Why so?
2. Same goes with interfaces, they cannot be private and protected. I understand that they cannot be private because no other class will be able to implement it then. But why not protected? What if I want only the classes in a particular package to implement an interface?
Classes can be both private and protected so long as they are defined as inner classes. The rule is that every class file must have exactly one public class declared. After which, though, any number of additional inner classes may be defined including anonymous/static classes.
Consider the following valid code:
You can also define any number of outter-level package classes such as TestFile2 as shown above.
Originally posted by Scott Selikoff: The rule is that every class file must have exactly one public class declared
It's not a must that a public class be declared in a compilation unit (i.e., a Java source file). There can be, at most, only one public class in it. You can also have one or more default access classes (package access) declared in a compilation unit.
A private top-level class would be of no use, because noone could ever access it.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus