Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't java main class be protected?

Tags:

java

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

like image 831
Santhosh Avatar asked Dec 04 '22 11:12

Santhosh


1 Answers

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.

like image 198
Jon Skeet Avatar answered Feb 08 '23 15:02

Jon Skeet