Access Modifiers in Java
Access Modifiers in Java
Practices)
Access modifiers in Java define the scope and accessibility of classes, methods, and variables,
ensuring data security, encapsulation, and proper code structuring. Java provides four primary
access modifiers:
3. protected – Accessible within the same package and subclasses in different packages.
These modifiers are crucial for object-oriented programming (OOP) as they provide data hiding,
controlled access, and modularity.
The private modifier allows access only within the same class. It prevents direct access to variables
and methods from outside the class, ensuring data security and encapsulation. Getter and setter
methods provide controlled access.
If no access modifier is specified, the member has default (package-private) access, meaning it is
accessible only within the same package but not from other packages. This is useful when multiple
related classes work together but should not be exposed to unrelated classes.
empId and display() can be accessed because they are in the same package. However, if we try to
access them from another package, a compilation error occurs.
The protected modifier allows access within the same package and in subclasses, even if they are in
different packages. This is useful in inheritance, where a subclass can access and modify behavior
from a parent class.
The message variable is protected, so it is accessible in the subclass Child, even though it is in a
different package. However, it cannot be accessed directly from an object in a different package.
The public modifier allows access from anywhere, including different packages and unrelated classes.
It is commonly used for APIs, libraries, and main classes that need to be accessed globally.
The Car class, model variable, and showModel() method are public, so they can be accessed from
any package.
Java only allows public and default (package-private) access modifiers for top-level classes. A class
cannot be private or protected at the top level because it would make the class inaccessible to other
parts of the program.
Only public and default classes are allowed at the top level.
Comparison Table of Access Modifiers
Subclass
Subclass (same Non-subclass Non-subclass
Modifier Class (different
package) (same package) (different package)
package)
Within
private
class
Within
default
package
Within
protected (if subclass)
package
public Anywhere
Use private for data hiding, default for package-level encapsulation, protected for inheritance,
and public for global access.
1. Use private for encapsulation and provide controlled access via getters and setters.
2. Use protected when designing a class for inheritance while preventing direct access.
3. Use default access to limit access within a package when working with related classes.
4. Use public cautiously, ensuring that only essential methods and classes are exposed globally.
Conclusion
Access modifiers are fundamental in Java for security, encapsulation, and object-oriented design. By
restricting access appropriately, developers can prevent unauthorized modifications, ensure code
modularity, and maintain clean and maintainable code. The correct use of access modifiers
enhances data integrity, reusability, and maintainability, making them a crucial aspect of Java
programming.