I can create Main class with Access specifier "public" or default.
But why can't I create with protected. As default itself allowed why not protected.
Public:
public class MainClass {
public static void main(String[] args) {
}
}
Default:
class MainClass {
public static void main(String[] args) {
}
}
Protected:
protected class MainClass {
public static void main(String[] args) {
}
}
Its showing error: Illegal modifier for the class MainClass; only public, abstract & final are permitted MainClass.java SCJP/src line 1 Java Problem
protected
relates to giving subclasses of the containing type access to a member. There's no containing type here, so what would it mean?
Note that this has nothing to do with main
as such... it applies to any top-level class. It is valid for a nested type to be protected though:
public class Foo {
protected static class Bar{}
}
This allows subclasses of Foo
to access Bar
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With