From the course: Nail Your Java Interview

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Add encapsulation to your programs to impress interviewers

Add encapsulation to your programs to impress interviewers - Java Tutorial

From the course: Nail Your Java Interview

Start my 1-month free trial

Add encapsulation to your programs to impress interviewers

- [Instructor] Another important object-oriented concept is encapsulation. Encapsulation is about binding an object's state and behaviors together. It's a way of wrapping data and code acting on the data into a single unit. In Java, this would be a class. To prevent classes from being tightly coupled, we want to keep related data and behaviors together inside of a single unit class. With encapsulation, you can make a class's variables and data hidden from other classes. The data would still be accessible, but only indirectly through specific methods of the class. This is known as data hiding. In Java, we can achieve this by declaring all the fields in a class as private and writing public methods in the class to get and set the values of these attributes. With this, other classes can still access the hidden data, but they can only do so through a public method of the given class. Let's take a look at an example in code.…

Contents