Introduction
Access modifiers in Java control the visibility and accessibility of classes, methods, and fields. They help in encapsulating the data and provide a way to control how other parts of the code can interact with these members. Understanding access modifiers is crucial for designing robust and secure Java applications.
Table of Contents
- What are Access Modifiers?
- Types of Access Modifiers
- Public Access Modifier
- Private Access Modifier
- Protected Access Modifier
- Default Access Modifier (No Modifier)
- Examples
1. What are Access Modifiers?
Access modifiers are keywords that set the access level for classes, methods, and fields. They determine which parts of the code can access a particular class or member. The main access modifiers in Java are public
, private
, protected
, and default (no modifier).
2. Types of Access Modifiers
Java provides four types of access modifiers:
- Public: The member is accessible from anywhere.
- Private: The member is accessible only within the class it is declared.
- Protected: The member is accessible within the same package and subclasses.
- Default (No Modifier): The member is accessible only within the same package.
3. Public Access Modifier
The public
access modifier makes a class, method, or field accessible from any other class.
Example: Public Access Modifier
public class Car {
public String model;
public String color;
public void displayDetails() {
System.out.println("Car model: " + model + ", Color: " + color);
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.model = "Toyota";
myCar.color = "Red";
myCar.displayDetails();
}
}
Output:
Car model: Toyota, Color: Red
In this example, the Car
class and its members are accessible from the Main
class because they are declared as public
.
4. Private Access Modifier
The private
access modifier restricts the visibility of a class member to within the class it is declared.
Example: Private Access Modifier
public class Car {
private String model;
private String color;
public void setModel(String model) {
this.model = model;
}
public void setColor(String color) {
this.color = color;
}
public void displayDetails() {
System.out.println("Car model: " + model + ", Color: " + color);
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.setModel("Honda");
myCar.setColor("Blue");
myCar.displayDetails();
}
}
Output:
Car model: Honda, Color: Blue
In this example, the model
and color
fields are private, so they can only be accessed and modified through the public methods setModel
and setColor
.
5. Protected Access Modifier
The protected
access modifier allows the member to be accessible within the same package and by subclasses.
Example: Protected Access Modifier
public class Vehicle {
protected String brand;
protected void displayBrand() {
System.out.println("Brand: " + brand);
}
}
public class Car extends Vehicle {
private String model;
public Car(String brand, String model) {
this.brand = brand;
this.model = model;
}
public void displayDetails() {
displayBrand();
System.out.println("Model: " + model);
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Toyota", "Corolla");
myCar.displayDetails();
}
}
Output:
Brand: Toyota
Model: Corolla
In this example, the brand
field and displayBrand
method are protected, so they are accessible in the Car
subclass.
6. Default Access Modifier (No Modifier)
When we do not mention any access modifier, it is called the default access modifier. The scope of this modifier is limited to the package only. This means that if we have a class with the default access modifier in a package, only those classes that are in this package can access this class. No other class outside this package can access this class.
Example: Default Access Modifier
class Car {
String model;
String color;
void displayDetails() {
System.out.println("Car model: " + model + ", Color: " + color);
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car();
myCar.model = "Ford";
myCar.color = "Green";
myCar.displayDetails();
}
}
Output:
Car model: Ford, Color: Green
In this example, the Car
class and its members have default access and are accessible within the same package.
7. Examples
Example: Combining Access Modifiers
public class BankAccount {
private String accountNumber;
private double balance;
// Public constructor
public BankAccount(String accountNumber, double balance) {
this.accountNumber = accountNumber;
this.balance = balance;
}
// Public method to deposit money
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
// Public method to withdraw money
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
}
}
// Public method to display account details
public void displayAccountDetails() {
System.out.println("Account Number: " + accountNumber + ", Balance: " + balance);
}
// Private method to check balance
private double getBalance() {
return balance;
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount("123456789", 1000.0);
account.deposit(500.0);
account.withdraw(200.0);
account.displayAccountDetails();
// The following line will cause an error because getBalance() is private
// double balance = account.getBalance();
}
}
Output:
Account Number: 123456789, Balance: 1300.0
In this example, different access modifiers are used to control access to the BankAccount
class members.
Conclusion
Access modifiers in Java are essential for encapsulating data and controlling the visibility of class members. They help in designing secure and maintainable applications. Understanding how to use public
, private
, protected
, and default access modifiers is crucial for effective Java programming.