Home > Java > javaTutorial > body text

In Java, can we declare a top-level class as protected or private?

WBOY
Release: 2023-09-12 19:21:03
forward
1408 people have browsed it

In Java, can we declare a top-level class as protected or private?

No , we cannot declare a top-level class as private or protected. It can be public or default ( no modifiers ). If there are no modifiers, there should be default access.

Syntax

// A top level class
   public class TopLevelClassTest {
      // Class body
}
Copy after login
If you declare a top-level class as private, the compiler will report an error and prompt "The modifier private is not allowed here." This means that top-level classes cannot be private, and the same applies to the protected access modifier. Protected means that the member can be accessed by any class in the same package as well as subclasses, even if they are in another package. Top-level classes can only have public, abstract, and final modifiers, or they may not define any class modifiers. This is called default/package access. We can declare inner class as private or protected but this is not allowed in outer class classes.
  • Multiple top-level classes can be defined in a Java source file, but there can be at most one public top-level class declaration. The file name must match the name of the public class.
  • Declare the class as Protected

    Example

    Live Demo

    protected class ProtectedClassTest {
       int i = 10;
       void show() {
          System.out.println("Declare top-level class as protected");
       }
    }
    public class Test {
       public static void main(String args[]) {
          ProtectedClassTest pc = new ProtectedClassTest();
          System.out.println(pc.i);
          pc.show();
          System.out.println("Main class declaration as public");
       }
    }
    Copy after login

    Above In the example, we can declare the class as protected, and it will throw an error , indicating that the modifier protected is not allowed to be used here . Therefore, the above code will not execute.

    Output

    modifier protected not allowed here
    Copy after login

    Declare class as private

    Example

    Live demonstration

    private class PrivateClassTest {
       int x = 20;
       void show() {
          System.out.println("Declare top-level class as private");
       }
    }
    public class Test {
       public static void main(String args[]) {
          PrivateClassTest pc = new PrivateClassTest();
          System.out.println(pc.x);
          pc.show();
          System.out.println("Main class declaration as public");
       }
    }
    Copy after login

    In the above example, we can declare the class as private and it will throw an error indicating that the modifier private is not allowed here . So the above code will not execute.

    Output

    modifier private not allowed here
    Copy after login

    The above is the detailed content of In Java, can we declare a top-level class as protected or private?. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:tutorialspoint.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!