In Java, access modifiers are keywords used to define the visibility and accessibility of classes, methods, constructors, and variables. They are used to specify the level of access control for these members, ensuring that data is protected and accessible only where needed.
There are four primary types of access modifiers in Java:
public
private
protected
In this comprehensive guide, we'll explore each of these access modifiers in detail, including their usage and best practices.
In Java, access modifiers are used to define the scope and visibility of classes, methods, and other members. They determine how other parts of a program can access these members, whether they can be accessed from other classes or packages, or whether they are restricted to certain areas of the code.
Access modifiers play a crucial role in encapsulation, one of the fundamental principles of object-oriented programming. By controlling access to class members, they help maintain a clear and manageable interface for your code and protect its internal implementation.
The public
access modifier allows the widest level of access. A member declared as public
can be accessed from any other class or package in your program. This means that there are no restrictions on access to that member.
public class Car {
public String model;
public void display() {
System.out.println("Model: " + model);
}
}
In the example above, the model
variable and the display()
method are both public
, meaning they can be accessed from anywhere, even from classes in other packages.
// In another class
Car car = new Car();
car.model = "Toyota"; // Accessing the public field
car.display(); // Calling the public method
Key Points:
public
are accessible from any other class, regardless of the package.The private
access modifier is the most restrictive access level. A member declared as private
can only be accessed within the same class in which it is declared. This is the core principle of encapsulation — keeping the internal workings of a class hidden from outside interference.
public class Car {
private String model;
public void setModel(String model) {
this.model = model;
}
public void display() {
System.out.println("Model: " + model);
}
}
In this example, the model
variable is private
, so it cannot be accessed directly from outside the Car
class. The only way to modify or retrieve the model
is through the setModel()
method.
// In another class
Car car = new Car();
car.setModel("Toyota"); // Setting the model using the public method
car.display(); // Displaying the model
Key Points:
private
are only accessible within the same class.The protected
access modifier allows access within the same package (like the default modifier) and also to subclasses (even if they are in different packages). This provides a balance between encapsulation and extending functionality in subclasses.
public class Car {
protected String model;
protected void display() {
System.out.println("Model: " + model);
}
}
In this example, the model
variable and the display()
method are both protected
, meaning they can be accessed within the Car
class, any class in the same package, or by subclasses, even if the subclass is in a different package.
// In a subclass (even if in a different package)
public class ElectricCar extends Car {
public void showModel() {
System.out.println("Electric Car Model: " + model);
}
}
Key Points:
protected
are accessible within the same package and to subclasses (even if they are in different packages).If no access modifier is specified, the default access level is package-private (also known as the default access modifier). This means that the member is accessible only within the same package. It cannot be accessed from classes in other packages.
public class Car {
String model; // Default access modifier (package-private)
void display() {
System.out.println("Model: " + model);
}
}
In this example, both the model
variable and the display()
method have the default access modifier, which means they are only accessible within classes in the same package.
// In another class in the same package
Car car = new Car();
car.model = "Toyota"; // Accessing the default variable
car.display(); // Calling the default method
Key Points:
Java classes can also have access modifiers. By default, classes can only have public
or default access modifiers. If you do not specify an access modifier, the class will have package-private access.
public
: A class marked as public
can be accessed from any other class, even if they are in different packages.
public class PublicClass { // Can be accessed from any package
// Class content
}
class DefaultClass { // Package-private: Can only be accessed within the same package
// Class content
}
Access modifiers can also be applied to methods. You can use public
, private
, protected
, or the default access modifier for methods to define who can access and invoke those methods.
public class Car {
private void privateMethod() {
System.out.println("This is a private method");
}
public void publicMethod() {
System.out.println("This is a public method");
}
protected void protectedMethod() {
System.out.println("This is a protected method");
}
}
Access modifiers can be used to define the visibility of variables as well. You can control who can read and modify the variables, making them an essential part of encapsulating data.
public class Car {
private String model; // Private variable
public void setModel(String model) {
this.model = model; // Setting the value of private variable
}
public String getModel() {
return model; // Accessing the private variable through a public method
}
}