Encapsulation in Java

Encapsulation, Polymorphism, and Inheritance are the core object oriented programming principles. In this blog, I will talk about encapsulation in Java.

Encapsulation is the mechanism that binds together code and the data it manipulates.

In Java, the basis of encapsulation is the Class. A class defines the structure and behavior (data and code) that is shared by a set of objects.

Encapsulation (information hiding) is achieved

  • By combining data and behavior in one package and hiding the implementation details from the users of the object.
  • The key to encapsulation is to have methods never directly access instance variables in a class other than their own.
  • Programs should interact with object data only through the objects methods. Declare all the variables in the class as private and have public Mutator methods and Accessor methods.

In the following program, all Client class variables are declared as private and can be accessed only with their Mutator and Accessor methods as shown by the test code.

// Java program to demonstrate encapsulation
public class Client {
// private variables declared
// these can only be accessed by
// public methods of class
private Portfolio portfolio;
private String first_Name;
private String last_Name;
private String location;
private int age;
public Client() {
portfolio=null;
first_Name=””;
last_Name=””;
location=””;
age=0;
}
public Portfolio getPortfolio() {
return portfolio;
}
public void setPortfolio(Portfolio portfolio) {
this.portfolio = portfolio;
}
public String getFirstName() {
return first_Name;
}
public void setFirstName(String name) {
this.first_Name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getLast_Name() {
return last_Name;
}
public void setLast_Name(String last_Name) {
this.last_Name = last_Name;
}
}
public class TestEncapsulation
{
public static void main (String[] args)
{
Client obj = new Client();
// setting values of the variables
obj.setFirstName(“Dan“);
obj.setLast_Name(“Burke“);
obj.setAge(19);
obj.setLocation(“America“);
// Displaying values of the variables
System.out.println(“Client’s name: “ + obj.getFirstName());
System.out.println(“Client’s age: “ + obj.getAge());
System.out.println(“Client’s roll: “ + obj.getLast_Name());
System.out.println(“Client’s roll: “ + obj.getLocation());
// Direct access of first_Name is not possible
// due to encapsulation
// System.out.println(“Client’s name: ” + obj.first_Name());
}
}
Output:
Dan
19
Burke
America
Advantages of Encapsulation:
  • Data Hiding: The user has no idea about the inner implementation of the class. The class is then safe from unrestricted manipulations, as the only access to the variables is through their getter and setter.
  • Increased Access Control: The variables of the class can be made read-only or write-only depending on the requirement. To make the variables read-only, omit the setter methods or to make the variables as write-only, omit the getter.
  • Reusability: Encapsulation enables reusability and redesign as per new requirements.
  • Testing code is easy: It is much easier to test encapsulated code with unit testing.

Read More

Related posts

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.