Access Modifiers
Access Modifiers
A school program needs reliable data and clear rules. Access modifiers control which code can use a declaration. A private mark allows the student class to check changes through a public method.
Understand
Access modifiers control which code can use a declaration.
Apply
Trace the Java example, predict its output and explain the result before running it.
Avoid mistakes
Access control is a language boundary, not encryption. A protected member has package and subclass rules, not universal visibility.
Access Modifiers
In Java, access modifiers are keywords that set the visibility or accessibility of classes, methods, constructors, and variables. They help define who can see or use a particular piece of code. Think of them like the security levels of a building—some areas are open to all, while others are restricted.
Access Modifiers promote encapsulation. By carefully selecting access levels, you keep your code modular, maintainable, and secure from unintended interference.
The Four Types of Access Modifiers in Java
- public – Accessible from anywhere
- private – Accessible only within the class
- protected – Accessible within the package and subclasses
- default (no modifier) – Accessible within the package only
1. public – Open to All
When a class or method is marked public, it’s like declaring it as “open for business.” Any class from any package can access it.
public class MyClass {
public void greet() {
System.out.println("Hello from a public method!");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.greet();
}
}
Hello from a public method!
3. protected – For Family and Friends
Protected members can be accessed from:
- Classes within the same package
- Subclasses even if they’re in different packages
// In package animals
package animals;
public class Animal {
protected void makeSound() {
System.out.println("Animal makes a sound");
}
}
// In package zoo
package zoo;
import animals.Animal;
public class Dog extends Animal {
public void bark() {
makeSound(); // Allowed due to protected
System.out.println("Dog barks!");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.bark();
}
}
Animal makes a sound
Dog barks!
Here, the subclass Dog accesses makeSound() from a different package because it's protected.
4. default (Package-Private) – Locally Visible
When no modifier is specified, Java treats it as default access. Such members are only visible within the same package.
// In file PackageClass.java
package mypackage;
class PackageClass {
void display() {
System.out.println("Default access: only in mypackage");
}
}
// In another file in same package
package mypackage;
public class Demo {
public static void main(String[] args) {
PackageClass pc = new PackageClass();
pc.display(); // Works fine
}
}
Default access: only in mypackage
If another class in a different package tries to use PackageClass or call display(), it will result in an error.
Access Modifier Summary Table
| Modifier | Same Class | Same Package | Subclass (Diff Package) | Other Packages |
|---|---|---|---|---|
| public | Yes | Yes | Yes | Yes |
| protected | Yes | Yes | Yes | No |
| default | Yes | Yes | No | No |
| private | Yes | No | No | No |
When to Use What?
Here’s a quick decision guide:
- Use private when data or logic must be hidden.
- Use default for internal package-level helpers.
- Use protected when building for inheritance.
- Use public for shared APIs and libraries.
Important boundaries and mistakes
Access control is a language boundary, not encryption. A protected member has package and subclass rules, not universal visibility.
Try the school example
Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: Access control is a language boundary, not encryption.
Java 17 · Main.java · complete practice program
class Record {
private int mark;
void setMark(int value) {
if (value < 0 || value > 100) throw new IllegalArgumentException("mark");
mark = value;
}
int getMark() {
return mark;
}
}
public class Main {
public static void main(String[] args) {
Record s = new Record();
s.setMark(72);
System.out.println(s.getMark());
}
}
Now change one thing: Try reading s.mark directly from Main. Predict the new behavior before checking the worked variation.
Worked answer and reasoning
Java 17 · Main.java · source repeated for this answer
class Record {
private int mark;
void setMark(int value) {
if (value < 0 || value > 100) throw new IllegalArgumentException("mark");
mark = value;
}
int getMark() {
return mark;
}
}
public class Main {
public static void main(String[] args) {
Record s = new Record();
s.setMark(72);
System.out.println(s.getMark());
}
}
Expected output
72Access modifiers control which code can use a declaration. A private mark allows the student class to check changes through a public method.
Access control is a language boundary, not encryption. A protected member has package and subclass rules, not universal visibility.
Run with javac Main.java, then java Main. The declaration inside Main keeps the helper type available to this standalone example.
Worked variation: Try reading s.mark directly from Main. The private field is inaccessible, so use getMark instead.
Check your understanding
What to remember
Access modifiers control which code can use a declaration. A private mark allows the student class to check changes through a public method. Access control is a language boundary, not encryption. A protected member has package and subclass rules, not universal visibility.
Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide and Java 17 language specification.
Reaching the recap and completing the quizzes are separate milestones.