In Java, methods and data members of a class/interface can have one of 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
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)
We cannot declare class/interface with private or protected access specifiers. For example, following program fails in compilation.
//filename: Main.java protected class Test {} public class Main { public static void main(String args[]) { } } |
Note : Nested interfaces and classes can have all access specifiers.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Why Java Interfaces Cannot Have Constructor But Abstract Classes Can Have?
- Access specifier of methods in interfaces
- Format Specifiers in Java
- Access and Non Access Modifiers in Java
- Format specifiers in different Programming Languages
- Interfaces in Java
- Functional Interfaces In Java
- Interfaces and Inheritance in Java
- Interfaces in Java
- Callback using Interfaces in Java
- Private Methods in Java 9 Interfaces
- Two interfaces with same methods having same signature but different return types
- Abstract Classes in Java
- Private Constructors and Singleton Classes in Java
- Primitive Wrapper Classes are Immutable in Java
- Parent and Child classes having same data member in Java
- Wrapper Classes in Java
- Classes and Objects in Java
- Nested Classes in Java
- Understanding Classes and Objects in Java
Improved By : ebrahimalvi2

