← Back to Core Java Course | Chapter 7: OOP Core | Lesson 5 of 11

Java Access Modifiers

What are Access Modifiers?

Access modifiers control which parts of your codebase are allowed to see and use a given class, field, or method, forming the basis of encapsulation — they're the mechanism that actually enforces the 'hide internal details' principle of OOP.

Example: What are Access Modifiers?

java
class Account {
	private double balance = 100;
	public double getBalance() {
		return balance;
	}
}
public class Main {
	public static void main(String[] args) {
		Account acc = new Account();
		System.out.println(acc.getBalance());
	}
}

The Private Modifier

private is the most restrictive level: a private member is visible only inside the class where it's declared, not even to subclasses. This is the default choice for internal implementation details you never want other code touching directly.

Example: The Private Modifier

java
class Account {
	private double balance = 100;
}
public class Main {
	public static void main(String[] args) {
		Account acc = new Account();
		// acc.balance would not compile: private to Account
		System.out.println("Balance is private");
	}
}

Default Access

Leaving off a modifier entirely gives a member default (package-private) access, meaning any class in the same package can use it, but classes in other packages cannot — even subclasses outside the package are locked out unless they also use protected or public.

Example: Default Access

java
class Account {
	double balance = 100; // no modifier: package-private
}
public class Main {
	public static void main(String[] args) {
		Account acc = new Account();
		System.out.println(acc.balance); // visible: same package
	}
}

The Protected Modifier

protected opens visibility to the declaring class's own package plus any subclass anywhere, even in a different package — it's the modifier of choice for members a subclass legitimately needs to inherit and use, without exposing them to unrelated code.

Example: The Protected Modifier

java
class Vehicle {
	protected String type = "Generic";
}
class Car extends Vehicle {
	void printType() {
		System.out.println(type); // subclass can access protected member
	}
}
public class Main {
	public static void main(String[] args) {
		new Car().printType();
	}
}

Visibility Control

Choosing the tightest access level that still lets your code work is a core defensive-programming habit: it keeps your class's internal state safe from accidental external modification while still exposing exactly the operations callers actually need.

Example: Visibility Control

java
class Account {
	private double balance = 100;
	public double getBalance() {
		return balance; // tightest access + controlled exposure
	}
}
public class Main {
	public static void main(String[] args) {
		System.out.println(new Account().getBalance());
	}
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.