Access Modifiers in Java
1. public 
The public access modifier allows the member to be accessible from any other class. There are no restrictions on the visibility of public members.
Example
  public class PublicExample {
    public int publicVariable = 10;
    public void publicMethod() {
        System.out.println("Public method called.");
    }
}
class Test {
    public static void main(String[] args) {
        PublicExample example = new PublicExample();
        System.out.println("Public Variable: " + example.publicVariable);
        example.publicMethod();
    }
}
  
  Explanation
- publicVariableand- publicMethodcan be accessed from any other class.
- Testclass accesses the public members of- PublicExample.
2. protected 
The protected access modifier allows the member to be accessible within the same package and by subclasses (including those in different packages).
Example
  package package1;
public class ProtectedExample {
    protected int protectedVariable = 20;
    protected void protectedMethod() {
        System.out.println("Protected method called.");
    }
}
package package2;
import package1.ProtectedExample;
public class SubclassExample extends ProtectedExample {
    public void display() {
        System.out.println("Protected Variable: " + protectedVariable);
        protectedMethod();
    }
    public static void main(String[] args) {
        SubclassExample example = new SubclassExample();
        example.display();
    }
}
  
  Explanation
- protectedVariableand- protectedMethodare accessible within the- package1package and by the- SubclassExampleclass in- package2.
- SubclassExampleextends- ProtectedExampleand accesses its protected members.
3. Default (No Modifier)
If no access modifier is specified, the member has default access, which means it is accessible only within the same package.
Example
  package package1;
class DefaultExample {
    int defaultVariable = 30; // Default access
    void defaultMethod() {
        System.out.println("Default method called.");
    }
}
  
  
  package package1;
public class TestDefault {
    public static void main(String[] args) {
        DefaultExample example = new DefaultExample();
        System.out.println("Default Variable: " + example.defaultVariable);
        example.defaultMethod();
    }
}
  
  Explanation
- defaultVariableand- defaultMethodare accessible only within the- package1package.
- TestDefaultclass in the same package can access these default members.
4. private 
The private access modifier restricts the member to be accessible only within the same class. It is the most restrictive access level.
Example
  public class PrivateExample {
    private int privateVariable = 40;
    private void privateMethod() {
        System.out.println("Private method called.");
    }
    public void accessPrivateMembers() {
        System.out.println("Private Variable: " + privateVariable);
        privateMethod();
    }
}
class Test {
    public static void main(String[] args) {
        PrivateExample example = new PrivateExample();
        example.accessPrivateMembers();
        // The following lines would cause compilation errors:
        // System.out.println("Private Variable: " + example.privateVariable);
        // example.privateMethod();
    }
}
  
  Explanation
- privateVariableand- privateMethodare accessible only within the- PrivateExampleclass.
- The accessPrivateMembersmethod allows access to private members within the same class.
- Attempts to access private members from outside the class (e.g., in Test) will result in compilation errors.
Summary of Access Modifiers
|—|—|—|—|—| |Modifier|Same Class|Same Package|Subclass (Different Package)|Any Class (Different Package)| |public|Yes|Yes|Yes|Yes| |protected|Yes|Yes|Yes|No| |Default|Yes|Yes|No|No| |private|Yes|No|No|No|