Java Access modifiers


Access modifiers in Java are keywords used to define the visibility or accessibility of classes, methods, variables, and constructors. They control how the members of a class can be accessed from other classes and packages. Java provides four access modifiers:

1. Public

  • Description: Members declared as public are accessible from any other class in any package.
  • Usage: Use public when you want to allow unrestricted access to a class member.
public class MyClass { public int myPublicVar; public void myPublicMethod() { System.out.println("Public method"); } }

2. Protected

  • Description: Members declared as protected are accessible within the same package and by subclasses (even if they are in different packages).
  • Usage: Use protected when you want to allow access to subclasses and classes in the same package.
public class MyClass { protected int myProtectedVar; protected void myProtectedMethod() { System.out.println("Protected method"); } }

3. Default (Package-Private)

  • Description: If no access modifier is specified, the member is accessible only within its own package (also known as package-private).
  • Usage: Use the default modifier when you want to restrict access to classes within the same package.
class MyClass { int myDefaultVar; // Default access void myDefaultMethod() { System.out.println("Default method"); } }

4. Private

  • Description: Members declared as private are accessible only within the class in which they are declared. They are not accessible from outside the class, even in subclasses.
  • Usage: Use private when you want to restrict access to class members to ensure encapsulation.
public class MyClass { private int myPrivateVar; private void myPrivateMethod() { System.out.println("Private method"); } }

Summary of Access Modifiers

ModifierClassPackageSubclassWorld
publicYesYesYesYes
protectedYesYesYesNo
(default)YesYesNoNo
privateYesNoNoNo

Example of Access Modifiers

Let’s look at a complete example to illustrate the use of access modifiers.

package com.example; public class AccessModifiersExample { public int publicVar = 10; // Public variable protected int protectedVar = 20; // Protected variable int defaultVar = 30; // Default variable private int privateVar = 40; // Private variable public void display() { System.out.println("Public Var: " + publicVar); System.out.println("Protected Var: " + protectedVar); System.out.println("Default Var: " + defaultVar); System.out.println("Private Var: " + privateVar); } } class TestAccessModifiers { public static void main(String[] args) { AccessModifiersExample example = new AccessModifiersExample(); // Accessing members System.out.println("Accessing members from TestAccessModifiers:"); System.out.println("Public Var: " + example.publicVar); // Accessible System.out.println("Protected Var: " + example.protectedVar); // Accessible (same package) System.out.println("Default Var: " + example.defaultVar); // Accessible (same package) // System.out.println("Private Var: " + example.privateVar); // Not accessible (private) example.display(); // Displays all variables } }

Output of the Example

When you run the above code, the output will be:

Accessing members from TestAccessModifiers: Public Var: 10 Protected Var: 20 Default Var: 30 Public Var: 10 Protected Var: 20 Default Var: 30 Private Var: 40

Key Points

  • Encapsulation: Access modifiers are crucial for encapsulation, allowing you to control access to class members and protect the internal state of an object.
  • Best Practices: As a best practice, make class members private by default and provide public or protected methods (getters and setters) for accessing or modifying them.
  • Code Maintainability: Proper use of access modifiers enhances code maintainability and readability by clearly defining the scope of class members.

Summary

Access modifiers are essential in Java for controlling the visibility and accessibility of class members. Understanding how to use them effectively can help create secure, maintainable, and organized code.