Basic Modifier Types in Java

Access modifiers specifies who can access them. There are four access modifiers used in java. They are public, private, protected, no modifier (declaring without an access modifier). Using ‘no modifier’ is also sometimes referred as ‘package-private’ or ‘default’ or ‘friendly’ access. Usage of these access modifiers is restricted to two levels. The two levels are class level access modifiers and member level access modifiers.

I) Class level access modifiers (java classes only)

Only two access modifiers is allowed, public and no modifier

II) Member level access modifiers (java variables and java methods)

All the four public, private, protected and no modifier is allowed.
For better understanding, member level access is formulated as a table:


Access Modifiers

Same Class Same Package Subclass Other packages
public Y Y Y Y
protected Y Y Y N
no access modifier Y Y N N
private Y N N N
First row {public Y Y Y Y} should be interpreted as:
Second row {protected Y Y Y N} should be interpreted as:
similarly interpret the access modifiers table for the third (no access modifier) and fourth (private access modifier) records.



<<Previous <<   || Index ||   >>Next >>


Labels: