What this topic covers
19 subtopics- Access modifiers6
- Design4
- Immutability4
- Python conventions4
- Defensive copies3
- Getters and setters3
- Leaky getters3
- Properties3
- Basics2
- Const correctness2
- Friends2
- Limits of access control2
Show all 19 subtopics
- Attributes1
- Benefits1
- Compile-time encapsulation1
- Invariants1
- Nested classes1
- Non-virtual interface1
- UML visibility1
- Beginner 15
- Intermediate 15
- Advanced 15
About this topic
Encapsulation keeps an object's data behind its methods, so the object can protect its invariants (a balance never negative, a date always valid) and change its internals without breaking callers. Access modifiers are the tool – Java has private, package (default), protected and public; C++ has private, protected and public plus friend; Python relies on conventions (_name) and name mangling (__name). The classic trap is a getter that hands out a mutable internal object, which quietly breaks encapsulation.
Free preview
Sample questions
Try each one first, then open the answer for the explanation and why the other options are wrong.
Why does this fail to compile?
class Account {
private double balance = 100;
double getBalance() { return balance; }
}
public class Main {
public static void main(String[] args) {
Account a = new Account();
a.balance = 1_000_000;
System.out.println(a.getBalance());
}
}
Show answer and explanation
Explanation
Private members are visible only inside their own class. Callers must go through methods such as deposit(), which can validate the change.
Why the other options are wrong
- AUnderscores in numeric literals are legal since Java 7.
- BPackage access is enough for Main in the same file.
- DAny field can be private.
The list field is private. What does this print?
import java.util.*;
public class Main {
static class Team {
private final List<String> members = new ArrayList<>(List.of("Asha"));
List<String> getMembers() { return members; }
}
public static void main(String[] args) {
Team t = new Team();
t.getMembers().add("Intruder");
t.getMembers().clear();
t.getMembers().add("Ravi");
System.out.println(t.getMembers());
}
}
Show answer and explanation
Explanation
private and final protect the reference, not the list it points to. The getter hands out the internal list, so callers can change it freely. Return List.copyOf(members) or Collections.unmodifiableList(members).
Why the other options are wrong
- Aclear() emptied it first.
- BAn ArrayList allows changes.
- DThe caller changed the internal list.
Outer and nested classes read each other's private members. What does this print?
public class Main {
private int secret = 7;
static class Helper {
private int hidden = 3;
int peek(Main m) { return m.secret; }
}
public static void main(String[] args) {
Main m = new Main();
Helper h = new Helper();
System.out.println(h.peek(m) + h.hidden);
}
}
Show answer and explanation
Explanation
A top-level class and everything nested in it form one unit of encapsulation: they can access each other's private members. The result is 7 + 3 = 10 (numeric addition, not string concatenation).
Why the other options are wrong
- AAccess is allowed inside the same top-level class.
- Bhidden is added too.
- CBoth operands are ints, so + adds.