Access Modifier as name implies is used to show how and where to access the classes and methods in a program.
Mostly Programming language have three access modifiers, but java have four access modifiers.
A Java access modifier specifies which classes can access a given class and its fields, constructors and methods. Access modifiers can be specified separately for a class, its constructors, fields and methods. Java access modifiers are also sometimes referred to in daily speech as Java access specifiers, but the correct name is Java access modifiers.
Classes, fields, constructors and methods can have one of four different Java access modifiers:
2) default (package)
Each of these Java access modifiers will be covered in the following sections.
Assigning an access modifier to a class, constructor, field or method is also sometimes referred to as "marking" that class, constructor, field or method as that which the access modifier specifies.
For example, apply the Java access modifier private to a method would be referred to as marking the method as private means it is called a private method.
Access Level of Access Modifiers
Understanding all java access modifiers
Level of reached of access modifiers in a simple table.
outside package by subclass only
It have highest level of accessibility and can be access anywhere ,in any package and by any program.
Example of public access modifier
It can be accessed within a same package or in any other (different) package but can be access only through inheritance.
Example of protected access modifier
If we does not apply any access modifier it will be called as default access modifier. for instance
void show() this method is treated as default access modifier.
Default access Modifier can be accessed same class or within same package .
Example of default access modifier
It can be accessed within same class only .It can not be used by other class or package.
Example of private access modifier
Where Access Modifiers can be used?
Class -Only public access modifier can be used on class.
private can be used in nested class or inner class methods ,constructors, fields - All modifiers(public,private,default,protected) can be apply.