Back to Java
2025-12-075 min read

Access Modifiers

Learn Access Modifiers step by step with clear examples and exercises.

Why This Matters

Java access modifiers determine how classes, methods, and variables can be accessed by other parts of a program. Understanding these modifiers is crucial for writing efficient and secure code. This lesson will delve into the three main access modifiers in Java: public, private, and protected, along with their practical uses and common mistakes to avoid.

Why This Matters

Access modifiers play a vital role in organizing and securing your Java code. By controlling the visibility of classes, methods, and variables, you can maintain encapsulation, ensure proper interaction between components, and prevent unauthorized access. These concepts are essential for writing robust and scalable applications, as well as for passing technical interviews and troubleshooting real-world bugs.

Prerequisites

Before diving into Java access modifiers, it's important to have a solid understanding of the following:

  1. Basic Java syntax (variables, methods, classes)
  2. Object-oriented programming concepts (encapsulation, inheritance, polymorphism)
  3. Package structure in Java (organizing classes and packages)

Core Concept

Public (public)

The public access modifier allows a class, method, or variable to be accessible from any other part of the program, including other packages and applications. This is the most permissive access level in Java.

// Example of a public class
public class MyClass {
// Example of a public method
public void myMethod() {
System.out.println("Hello, World!");
}
}

Private (private)

The private access modifier restricts access to a class, method, or variable within the defining class only. This is useful for encapsulation and data hiding, as it prevents other classes from directly interacting with private members.

// Example of a private variable
private int myVariable;

// Example of a private method that can only be called by other methods in MyClass
private void privateMethod() {
System.out.println("This method is private.");
}

Protected (protected)

The protected access modifier allows a class, method, or variable to be accessible within the defining package and any subclasses of the current class, regardless of their packages. This is useful for inheritance and polymorphism, as it enables subclasses to access and override protected members.

// Example of a protected method that can be accessed by subclasses in other packages
protected void protectedMethod() {
System.out.println("This method is protected.");
}

Default (no modifier)

If no access modifier is specified, a class, method, or variable will have default access, which means it can only be accessed within the defining package. This is useful for organizing related classes and methods within a package while maintaining encapsulation with private members.

// Example of a default class that can only be accessed by other classes in the same package
class DefaultClass {
// Example of a default method that can only be called by other classes in the same package
void defaultMethod() {
System.out.println("This method has default access.");
}
}

Worked Example

Let's consider an example of a Person class with public, private, and protected members:

public class Person {
// Public properties (visible to all)
public String name;
public int age;

// Private properties (only accessible within the class)
private String secret;
private double salary;

// Protected property (accessible within the package and subclasses)
protected String address;

// Constructor that initializes public and private properties
public Person(String name, int age, String secret) {
this.name = name;
this.age = age;
this.secret = secret;
}

// Public method to display information about the person
public void showInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}

// Private method that can only be called by other methods within the class
private void hideInfo() {
System.out.println("Secret: " + secret);
}
}

In this example, the Person class has public properties (name and age), private properties (secret and salary), and a protected property (address). The class also includes constructors, methods for accessing and modifying its members, and a method for displaying information about the person.

Common Mistakes

  1. Forgetting to specify an access modifier: If you don't explicitly specify an access modifier, your class, method, or variable will have default access within the package.
  2. Overusing public access modifiers: Excessive use of public access modifiers can lead to code that is difficult to maintain and secure, as it exposes too many implementation details to other parts of the program.
  3. Incorrectly using protected access modifiers: Protected members should only be used when necessary for inheritance and polymorphism, as they can potentially introduce complexity and security risks if misused.
  4. Ignoring encapsulation principles: Failing to use private members and methods appropriately can result in code that is difficult to understand, maintain, and extend.
  5. Forgetting about package structure: Organizing your classes and packages effectively can help you manage dependencies, avoid naming conflicts, and improve the overall maintainability of your Java projects.

Practice Questions

  1. Given the following class definition, what is the access level of the myMethod?
public class MyClass {
public void myMethod() {}
}

Answer: The access level of myMethod is public.

  1. What is the difference between private and protected members in Java?

Answer: Private members are only accessible within the defining class, while protected members can be accessed by any class within the same package or a subclass of the current class, regardless of its package.

  1. How can you make a class's methods and variables only accessible to other classes within the same package?

Answer: By not specifying an access modifier (default access), the class's members will be accessible only to other classes within the same package.

FAQ

Can I change the access level of a method or variable after it has been declared?

Answer: No, you cannot change the access level of a method or variable once it has been declared. However, you can create new methods with different access levels that call and manipulate existing members.

What happens if I try to access a private member from another class?

Answer: If you attempt to access a private member from another class, the compiler will generate an error, as private members are only accessible within the defining class.

Can I make a method public but still hide its implementation details by using private variables?

Answer: Yes, you can use private variables and methods within a public class to encapsulate implementation details while still exposing useful functionality through public methods. This is an essential principle of object-oriented programming known as encapsulation.

Access Modifiers | Java | XQA Learn