The Wayback Machine - https://web.archive.org/web/20220417214310/https://www.geeksforgeeks.org/access-specifiers-for-classes-or-interfaces-in-java/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Access specifiers for classes or interfaces in Java

  • Difficulty Level : Easy
  • Last Updated : 23 Feb, 2022

In Java, methods and data members can be encapsulated by the following four access specifiers. The access specifiers are listed according to their restrictiveness order. 
1) private (accessible within the class where defined) 
2) default or package-private (when no access specifier 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 specifiers when declared outside any other class. 
1) public 
2) default (when no access specifier is specified)

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

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!