Access modifiers in Java are keywords that define the visibility or accessibility of class members such as variables and methods. By using access modifiers, you can control the scope and availability of these members within the class and in other parts of the program. Java provides three main access modifiers: public, private, and protected.
The public access modifier is the most permissive modifier. When a member is declared as public, it can be accessed by any other class or object, regardless of the package it belongs to. This means that public members have the widest visibility and can be accessed from anywhere in the program.
For example, consider a class Person
with a public method getName()
:
public class Person {
public String getName() {
return "John Doe";
}
}
Now, in any other class or package, you can access the getName()
method using an object of the Person
class:
Person person = new Person();
String name = person.getName(); // Accessing a public method
The private access modifier is the most restrictive modifier. When a member is declared as private, it can only be accessed within the same class. Private members are not visible outside the class, even in subclasses or other classes within the same package.
Let's modify the Person
class to include a private member variable age
:
public class Person {
private int age;
public void setAge(int age) {
this.age = age;
}
}
In this example, the age
variable can only be accessed within the Person
class. Any attempt to access or modify it from outside the class will result in a compilation error.
Person person = new Person();
person.age = 25; // Compilation error: age has private access in Person
However, you can provide public methods to set or get the value of private members, enabling controlled access to them:
public class Person {
private int age;
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return age;
}
}
Now, you can set and retrieve the value of the age
variable using the public methods:
Person person = new Person();
person.setAge(25);
int age = person.getAge();
The protected access modifier provides access to members within the same package or subclass. This means that protected members are visible to subclasses and other classes within the same package.
Consider a class Vehicle
with a protected member variable fuelLevel
:
public class Vehicle {
protected double fuelLevel;
}
In this case, the fuelLevel
variable can be accessed within the Vehicle
class and any subclasses of Vehicle
. It is not visible outside the package unless a subclass inherits it.
public class Car extends Vehicle {
public void refuel() {
fuelLevel = 100; // Accessing a protected variable in the superclass
}
}
Subclasses like Car
can freely access the fuelLevel
variable inherited from the Vehicle
class.
Access modifiers in Java play a crucial role in providing encapsulation and controlling the visibility of class members. By using the public, private, and protected modifiers, you can fine-tune the accessibility of variables and methods. Public members allow unrestricted access, private members are limited to the class they are defined in, and protected members offer visibility within the same package and subclasses. Understanding and applying access modifiers effectively ensures proper encapsulation and enhances code maintainability and security in Java programming.
noob to master © copyleft