Java Access Modifiers
In Java, access modifiers control the visibility and accessibility of classes, methods, and variables within an application. Understanding these modifiers is crucial for building secure and efficient software, especially in domains like ride-sharing where data integrity and user privacy are paramount.
1. Private
The private access modifier restricts access to members within the same class. This ensures sensitive data is protected and accessed only through controlled methods.
public class Rider {
private String riderId;
private String name;
private void updateLocation(double latitude, double longitude) {
// Update rider's location
}
public void requestRide() {
updateLocation(37.7749, -122.4194); // Accessing private method within the class
// Logic for requesting a ride
}
}In this example, riderId, name, and updateLocation() are private. They can only be accessed within the Rider class, ensuring data privacy and encapsulation.
2. Protected
The protected access modifier allows access to members within the same package and by subclasses, enabling inheritance and specialization.
package vehicles;
public class Vehicle {
protected String vehicleId;
protected double currentLatitude;
protected double currentLongitude;
protected void move(double latitude, double longitude) {
// Move the vehicle to the specified location
}
}
public class Car extends Vehicle {
public void driveToDestination(double destinationLatitude, double destinationLongitude) {
move(destinationLatitude, destinationLongitude); // Accessing protected method from superclass
// Logic for driving to the destination
}
}Here, vehicleId, currentLatitude, currentLongitude, and move() are protected, allowing subclasses like Car to access and utilize them for specific functionalities.
3. Default (No Modifier)
The default (package-private) access modifier limits access to members within the same package, ideal for internal functionalities not meant to be exposed externally.
package ride;
class RideUtilities {
void calculateFare() {
// Calculate fare based on distance and time
}
}
public class RideService {
void processRide() {
new RideUtilities().calculateFare(); // Accessing default class within the same package
// Additional logic for processing the ride
}
}In this example, RideUtilities is default (package-private), restricting access to classes within the ride package and promoting encapsulation of internal ride-related functionalities.
4. Public
The public access modifier makes members accessible from any other class, facilitating interactions with external components of the application.
package user;
public class UserService {
public void registerUser(String name, String email, String password) {
// Register a new user with the provided details
}
public boolean login(String email, String password) {
// Validate user credentials and login
return true;
}
}Here, registerUser() and login() are public, allowing them to be accessed from any part of the application, such as user registration and authentication functionalities.
Get Chowdhury Md Rajib Sarwar’s stories in your inbox
Join Medium for free to get updates from this writer.
In conclusion, mastering access modifiers in Java is essential for building robust and secure ride-sharing applications like Uber. Through the strategic use of `private`, `protected`, default (package-private), and `public` modifiers, developers ensure data integrity, encapsulation, and controlled access within their codebase.







