public
:- The
public
modifier allows the class, method, or field to be accessed from any other class. - If a class or interface is marked as
public
, it must be saved in a file with the same name as the class or interface and should be declared in that file. - Example:
- public class PublicExample { public int publicField; public void publicMethod() { // Method implementation } }
- The
private
:- The
private
modifier restricts the access of a class, method, or field to only within its own class. - It provides the highest level of encapsulation.
- Example:
- public class PrivateExample { private int privateField; private void privateMethod() { // Method implementation } }
protected
:- The
protected
modifier allows the class, method, or field to be accessed within its own package or by subclasses, even if they are in different packages. - Example:
- package mypackage; public class ProtectedExample { protected int protectedField; protected void protectedMethod() { // Method implementation } }
default
(no modifier):- If no access modifier is specified, the default access level is package-private.
- Package-private means that the class, method, or field is accessible only within its own package.
- Example:
- package mypackage; class DefaultExample { int defaultField; void defaultMethod() { // Method implementation } }
Here's an example that demonstrates the use of access modifiers in a more comprehensive way:
package com.example;
public class AccessModifierExample {
// Public field accessible from anywhere
public int publicField;
// Private field accessible only within this class
private int privateField;
// Protected field accessible within this package and by subclasses
protected int protectedField;
// Package-private (default) field accessible only within this package
int defaultField;
// Public method accessible from anywhere
public void publicMethod() {
// Accessing all fields is allowed within the same class
this.publicField = 1;
this.privateField = 2;
this.protectedField = 3;
this.defaultField = 4;
// Method implementation
}
// Private method accessible only within this class
private void privateMethod() {
// Method implementation
}
// Protected method accessible within this package and by subclasses
protected void protectedMethod() {
// Method implementation
}
// Package-private (default) method accessible only within this package
void defaultMethod() {
// Method implementation
}
}
Placing access modifier before class:
public
Modifier:- If the
public
modifier is placed before a class, it means that the class is accessible from any other class. This class can be accessed from any package, and instances of this class can be created and used freely. - Example:
- If the
- public class PublicClass { // Class members and methods }
default
(no modifier):
- If no access modifier is specified before a class (i.e., the default modifier), it means that the class is accessible only within its own package. This is also known as package-private or package-local access.
- Example:
class DefaultClass {
// Class members and methods
}
final
Modifier:
- While not an access modifier, it's worth mentioning that the
final
modifier can be used before a class to indicate that the class cannot be subclassed (i.e., it cannot have any subclasses). - Example:
public final class FinalClass {
// Class members and methods
}
It's important to note that placing protected
or private
modifiers before a top-level class is not allowed in Java. These modifiers are reserved for members (fields and methods) within a class.
Here's an example to illustrate:
// This is not allowed for top-level classes
// private class PrivateClass {} // Error: modifier private not allowed here
// protected class ProtectedClass {} // Error: modifier protected not allowed here
In summary, when an access modifier is placed before a class, it determines the visibility of the class to other classes and packages. The most commonly used modifiers for classes are public
and the default (package-private) modifier.