What are access modifiers and non-access modifiers in Java?
Java provides two types of modifiers.
- The private access modifier is the most restrictive access level. Class and interfaces cannot be private. variables that are declared private can be accessed outside the class.
- The public access modifier can be associated with class, method, constructor, interface, etc. public can be accessed from any other class. Therefore, fields, methods, blocks declared inside a public class can be accessed from any class.
- The protected access modifier can be associated with variables, methods, and constructors, which are declared protected in a superclass can be accessed only by the subclasses in other package or any class within the package of the protected members' class.
- The default access modifier does not have keyword A variable or method declared without any access control modifier is available to any other class in the same package.
Non Access Modifiers
- The static modifier for creating class methods and variables.
- The final modifier for finalizing the implementations of classes, methods, and variables.
- The abstract modifier for creating abstract classes and methods.
- The synchronized and volatile modifiers, which are used for threads.
Published on 24-Apr-2018 12:54:55