HomeJavaEncapsulation In 2 Easy Steps

Encapsulation In 2 Easy Steps

What is Encapsulation

In object-oriented programming (OOP), encapsulation is one of the basic principles. It explains the concept of bundling data and methods within one unit, e.g. a class in Java.

Encapsulation is one of the core principles of the Object-Oriented Programming (OOP) paradigm. It is the method of encoding the data stored in the member variables of the class with its member functions.

This concept is most frequently used to hide an object’s internal representation, or state, from the outside. This is called hiding of information(information hiding). If you have an attribute that is not visible from the outside of an object, then you can hide specific information and monitor access to the internal state of the object if you package it with methods that have read or write access to it.

If you are familiar with some object-oriented programming language, these methods are possibly known to you as the getter and setter methods. As the names mean, an attribute is obtained by a getter process and set by a setter process. You will determine whether an attribute can be read and modified, or whether it is read-only, or if it is not available at all, based on the methods that you enforce. You may also use the setter process, which I will teach you later, to enforce additional validation rules to ensure that your object still has a valid state.

Let’s take a look at an example that illustrates the principle of encapsulation and how you can use it to hide details and apply additional confirmation before modifying the values of the object attributes.

Benefits of data hiding

Security: The main advantage of encapsulation is the security of the data that comes with it since it can no longer be accessed directly from outside the class.

Control : Encapsulation allows the creator complete control of what is stored in the member variables.

Flexibility: Good design approaches, such as encapsulation, make the code portable, making it easy to modify and manage the code.

Example of Encapsulation

package com.onurdesk.encapsulation
public class Employee{
 
	private int employeeId;
	private String employeeName;
	private String department;
 
	public int getEmployeeId() {
		return employeeId;
	}
	public void setEmployeeId(int employeeId) {
		this.employeeId = employeeId;
	}
	public String getEmployeeName() {
		return employeeName;
	}
	public void setEmployeeName(String employeeName) {
		this.employeeName = employeeName;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	
	public static void main(String args[])
	{
		Employee employee =new Employee();
		employee.setEmployeeId(1);
		employee.setEmployeeName("Navjyot");
		employee.setDepartment("IT");
		
		System.out.println("******************");
		System.out.println("Employee Id: "+employee.getEmployeeId());
		System.out.println("Employee Name: "+employee.getEmployeeName());
		System.out.println("Employee Department: "+employee.getDepartment());
	}
}
 

When you are running the above code :

******************
Employee Id: 1
Employee Name: Navjyot
Employee Department: IT

Admin
Onurdesk is a technical platform based primarily on Spring, Java, and Nodejs ecosystem app creation. We write tutorials and manuals concentrating extensively on realistic and real-life usage cases.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

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

Most Popular

Recent Comments