Discuss the access specifiers in Java and their types.
To define the access scope of class, method, & variables, access specifiers are used in java. Following are the four access specifiers in java:
- Public: When apply on variables, methods and classes then can be accessible to everywhere means accessible by any method and class.
- Protected: Accessible in the same package or sub-class of this class and also accessible within the class.
- Default: When a class, variable or method is defined as default then they are accessible within the package only.
Note – by default, all the method, classes and variables are default. If you define nothing then it is consider as default.
- Private: When apply on variable, class or methods then all are accessed only within the class.
Must Read – How to run your java program?