ProLesson 4.3Beginner3 min readUpdated 30 October 2026
Encapsulation and access modifiers
Protect an object's data with private fields and safe methods, learn the four access levels, and know when getters and setters help or hurt.
Encapsulation means an object keeps its data private and only lets the outside world change it through methods that enforce the rules. A bank account shouldn't let anyone write balance = -1000000.
01The problem with public fields
java
class Account {
double balance; // anyone can do account.balance = -500;
}Nothing stops other code from putting the object into a nonsense state.
Loading the rest of the lesson…