Encapsulation

When you design classes for an application, the number of instance variables and methods in class can be huge. You will also realize that not all of them are required for an end user. Some of the instance variables or methods represent internal structure of the object which is not required for an end user. The end user shouldn’t have access to those members and one should aim to hide these members which we call as encapsulation in object oriented programming language. In Java, we achieve encapsulation by using access modifiers.

The most basic access modifiers in Java are public and private. (In the Access Modifiers sections we’ll see couple of more access modifiers)

If class member is marked as public, that means it can be accessible through a reference variable anywhere in the program. But if a class member is marked as private, it’s only accessible from within the class where it has been declared, you cannot access it outside of the class using object’s reference variable.

package com.techstackjournal.app.types;

public class Employee {

	private String id;
	private String name;
	private double averageOfRating = 3.5;
	private double salary;
	private double bonus;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.bonus = salary * (averageOfRating * 20) / 100;
		this.salary = salary;
	}

	public double getBonus() {
		return bonus;
	}

}

Let’s go through this hypothetical example. In this Employee class, we declared all the instance variables as private. So you cannot access them directly. But we allowed full access on id, name and salary using get and set methods. On bonus we allowed only get access. However, for averageOfRating, we didn’t provided get or set access. It is completely restricted for internal usage. This way we can define controlled access on the instance variables using access modifiers.

Instance variable averageOfRating forms internal structure of the object as it is used in calculation of bonus of the employee. But we felt that it is not required to expose this variable, so we declared it as private and didn’t provided get or set methods preventing access to this variable outside the class.

package com.techstackjournal.app.main;

import com.techstackjournal.app.types.Employee;

public class Application {

	public static void main(String[] args) {

		Employee emp = new Employee();
		emp.setName("John");
		emp.setId("E001");
		emp.setSalary(8000);

		System.out.println("Bonus: " + emp.getBonus());

	}

}
Bonus: 5600.0

Points to remember

  • Preventing or hiding access to the internal structure of the object is called Encapsulation.
  • We can achieve encapsulation using access modifiers
  • The most basic access modifiers in Java are – public and private
  • Members declared using public are accessible outside of the class via reference variable of the object
  • Members declared using private are accessible only within the class
  • By declaring all instance variables as private and providing access only through get and set methods we can define controlled access on the object

Leave a Comment