Object-Oriented Programming (OOP) – Access Modifiers (public, private, protected)
Introduction
Access modifiers in object-oriented programming (OOP) play a vital role in controlling the visibility and accessibility of class members (fields, methods, and inner classes). Java provides three main access modifiers: public, private, and protected. Each modifier serves a distinct purpose in encapsulating and securing class members. In this guide, we’ll explore these access modifiers and their practical applications in Java.
What are Access Modifiers?
Access modifiers are keywords in Java that define the accessibility of class members. They determine which parts of a class can be accessed from outside the class and in what way. The main access modifiers in Java are:
- Public: Members declared as public are accessible from any class or package.
- Private: Members declared as private are only accessible within the same class.
- Protected: Members declared as protected are accessible within the same package and by subclasses, even if they are in a different package.
Public Access Modifier
The ‘public’ access modifier is the most permissive. Members declared as ‘public’ are accessible from any class or package. This modifier is often used for methods and fields that need to be visible and accessible to all parts of a program.
public class PublicExample {
public int publicField;
public void publicMethod() {
// Method logic here
}
}
In this example, the ‘publicField’ and ‘publicMethod’ can be accessed from any other class in any package.
Private Access Modifier
The ‘private’ access modifier restricts access to the members within the same class. It is used to encapsulate and hide the internal details of a class, making them inaccessible from other classes.
public class PrivateExample {
private int privateField;
private void privateMethod() {
// Method logic here
}
}
In this example, ‘privateField’ and ‘privateMethod’ can only be accessed within the ‘PrivateExample’ class.
Protected Access Modifier
The ‘protected’ access modifier allows access within the same package and by subclasses, even if they are in different packages. It is often used when you want to provide limited access to class members while maintaining a level of encapsulation.
public class ProtectedExample {
protected int protectedField;
protected void protectedMethod() {
// Method logic here
}
}
In this example, ‘protectedField’ and ‘protectedMethod’ can be accessed by classes in the same package and subclasses of ‘ProtectedExample,’ regardless of their package.
Choosing the Right Access Modifier
Selecting the appropriate access modifier is crucial for designing clean and secure class structures. Here are some guidelines to help you decide:
- Public: Use ‘public’ when you want a member to be accessible from anywhere in your codebase.
- Private: Use ‘private’ when you want to encapsulate and hide the implementation details of a class from external code. It enhances security and maintains a well-defined interface.
- Protected: Use ‘protected’ when you want to allow limited access within the same package and by subclasses while still maintaining some level of encapsulation.
Access Modifiers in Inheritance
Access modifiers also play a significant role in inheritance. When a subclass inherits from a superclass, it inherits the superclass’s ‘public’ and ‘protected’ members but cannot access the ‘private’ members. This ensures that the encapsulation of the superclass is maintained.
Conclusion
Access modifiers are essential for controlling the visibility and accessibility of class members in Java. They allow you to encapsulate and secure your classes while providing the necessary exposure for other parts of your program. Choosing the right access modifier is a fundamental part of designing well-structured and maintainable software.