Encapsulation with Java

 

Encapsulation is an object-oriented programming feature that makes it possible to hide the state of an object from misuse. This is done by allowing access to the fields that we want to hide only through methods, not direcly using . operator preceded by the object name. As an object oriented language, Java fully supports encapsulation. Encapsulation in Java is achieved by declaring the instance variables that we want to hide as private and allowing access to those variables only through public methods.

Let’s go back to the Person class. So far, we have defined the fields of the Person class public so they could be accessed directly once a Person object was initialized. However, this exposes the fields of the Person class to misuse by any other class that the Person class is visible to. Since public fields are accessible to all the other classes, any class that has access to Person can change the state of a Person object directly through the instance. Now, let’s change the access modifiers of the instance fields to be private and allow access to the fields only through public methods.

public Class Person {

private String firstName;

private String lastName;

private int age;

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getFirstName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public String getLastName() {

return lastName;

}

public void setAge(int age) {

this.age = age;

}

public int getAge() {

return age;

}

}

Now, the Person class follows Java Beans standards and the fields firstName, lastName and age cannot be accessed directly through a Person instance. Any call that aims to get the value of a field or change the value of a field needs to go through a setter or getter. We will talk about Java Beans standards after object-oriented programing features.

Now, let’s define a new class named Paper. Paper can be printed on and the text that is printed on the paper can have different colors. Accordingly, we will define text and color as fields of the Paper object and we will allow changing the text on the page and color of the text.

public class Paper {

private String color;

private String text;

public Paper() {

color = “blue”;

text = “Welcome”;

}

public void setColor(String color) {

this.color = color;

}

public String getColor() {

return color;

}

public void setText(String text) {

this.text = text;

}

public String getText() {

return text;

}

public void textTextInfo() {

System.out.println(“The text on the page is ” + text + ” and its color is ” + getColor());

}

public static void main(String [] args) {

Paper paper = new Paper();

printTextInfo();

System.out.println(“Changing the text to Hello World.”);

paper.setText(“Hello World”);

printTextInfo();

System.out.println(“Changing the text color to green and changing the text to Great”);

printTextInfo();

}

}

The color and text fields of the page can be accessed only through getter and setter methods. This helps keep the Paper class’ state safe from misuse. In other words, text and color properties of the Paper class that define the behavior are encapsulated inside the class. This is a perfect example how encapsulation is implemented in Java.