Pages

What Is Encapsulation In Java?

Many times when you will go to attend WebDriver Interview on 2+ years experience, Interviewer will ask you about Encapsulation. Best answer on Encapsulation In Java software development language Is as bellow.

Encapsulation

Encapsulation In Java software development language Is one of the OOP fundamental concept. It Is process of packing code and data together In to a single Unit. Best example Is capsule where powder of several medicines Is packed In single unit.

It Is also known as "data hiding" technique as fields of class are declared private and providing access to the fields via public methods. So anyone can not access private data fields directly outside the class. We can use setter and getter methods to get and set data In to It.

Advantages of Encapsulation

Few advantages of encapsulation.

It Increase the usability.

It promotes the easy maintenance in java software development.

It makes the class easy to use for clients.

Data access and modification control.

It Is easy to change one part of code without affecting other part of code.

Example of Encapsulation

Let's look at very simple example of encapsulation In java software development language . I have created encapsulation class Encaps.java as bellow.