Tuesday, October 7, 2025
HomeLanguagesJavaAccess modifiers for classes or interfaces in Java

Access modifiers for classes or interfaces in Java

In Java, methods and data members can be encapsulated by the following four access modifiers. The access modifiers are listed according to their restrictiveness order. 
1) private (accessible within the class where defined) 
2) default or package-private (when no access modifier is specified) 
3) protected (accessible only to classes that subclass your class directly within the current or different package)
4) public (accessible from any class)

But, the classes and interfaces themselves can have only two access modifiers when declared outside any other class. 
1) public 
2) default (when no access modifier is specified)

Note: Nested interfaces and classes can have all access modifiers.
Note: We cannot declare class/interface with private or protected access modifiers.

For example, the following program fails in the compilation.

Java




//filename: Main.java
protected class Test {}
  
public class Main {
  public static void main(String args[]) {
  
  }
}


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6709 POSTS0 COMMENTS
Nicole Veronica
11874 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11937 POSTS0 COMMENTS
Shaida Kate Naidoo
6832 POSTS0 COMMENTS
Ted Musemwa
7091 POSTS0 COMMENTS
Thapelo Manthata
6781 POSTS0 COMMENTS
Umr Jansen
6785 POSTS0 COMMENTS