Advantages of Encapsulation in Java - Selenium Tutorials Beginners

The world “Encapsulation” is used in java to represent the mechanism wrapping the data and the code acting on the data into a single unit. This is one of the four major OOPs concepts in java the other three being Inheritance, Polymorphism, and abstraction. In encapsulation, the variables in a class can be accessed only through the methods of their present class and they will be hidden from other classes. Hence this mechanism is also known as Data Hiding.

Here is an example for Encapsulation in Java

/* File name : */
public class EncapTest {
  private String name;
  private String idNum;
  private int age;

  public int getAge() {
     return age;

  public String getName() {
     return name;

  public String getIdNum() {
     return idNum;

  public void setAge( int newAge) {
     age = newAge;

  public void setName(String newName) {
     name = newName;

  public void setIdNum( String newId) {
     idNum = newId;

Advantages of Encapsulation in Java

  • Encapsulation in java makes it easy to understand and maintain real-world entities by modeling them. This makes the programming faster.
  • Accessing data or modifying data can be controlled.
  • Encapsulation in java helps the clients to use the class easily.
  • Reusability can be enhanced using encapsulation in java.
  • It supports flexibility of design.
  • The fields of a class can be made a read or write only-only.
  • Total control over the data stored in a class will be within the class.

As mentioned in the beginning of the article, encapsulation in java is also known as Data Hiding as it controls the visibility of the internal data to the outside world. Java provides four different types of modifiers to control the visibility of the data stored in Class.

  • Private: – Which is the most rigid one and allows the visibility only up to the class level.
  • Default: – By default, the visibility of data will be upto package level.
  • Protected: – Here you will get access to the package level and also upto the subclass level.
  • Public: – It is the most flexible one and least restrictive one.

Java is fast becoming the most widely used programming language in the world and hence the demand for java programmers is increasing. At this juncture, many Java tutorials have come up offering training in java. Encapsulation in Java is one of the most important parts of any java tutorial and the java videos, which are provided as study material will depict the processes in detail with the help of infographics.

Leave a Reply

Your email address will not be published. Required fields are marked *