Java ClassesLesson 32Free preview
Java Encapsulation
This lesson is part of Java · All levels
Summary
Encapsulation is a technique of packing data within a class and restricting access to it with a private modifier. The outside parties do not change the data directly, but rather through a getter and setter method that can validate. This protects data from unreasonable changes and simplifies code handling.
Key points
- Make the field private and then provide the getter and the setter as the access gate.
- Setter can add validation before the value is written to the field
- The encapsulation hides the internal details and only displays what is needed
- Key advantage: data is safer and the changes within the class do not interfere with the other classes
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.