access modifiers in java

Example of private access modifier:

class Demo
   private void display()
      System.out.println("Hello world java");
public class PrivateAccessModifierExample
   public static void main(String[] args)
      Demo obj = new Demo();
For top level class only two access modifiers are allowed. 
public and default. If a class is declared as public it is visible everywhere.
If a class is declared default it is visible only in same package.
If we try to give private and protected as access modifier to class 
we get the below compilation error.
Illegal Modifier for the class only public,abstract and final are permitted.

New to Communities?

Join the community