Image: Encapsulation in JAVA
Image: Encapsulation in JAVA

In this article, Edureify will discuss another OOP concept – “Encapsulation”. OOP has four pillars namely, abstraction, encapsulation, polymorphism, and inheritance. Visit the Edureify website for more introduction to OOPs concepts.

While abstraction is used to expose only the relevant details to the end-user, encapsulation mainly deals with data security. In ensuring data security, encapsulation shields the data members from unwanted access by specifying access modifiers and also bundles the data into a single unit.

What Is Encapsulation In Java?

Using encapsulation we can also hide the class data members (variables) from the other classes. These data member variables can be accessed indirectly using the methods of the class in which they are declared. The methods in turn are accessed using the object of that class. Download the Eduriefy app now for more coding-related programs.

So what we conclude from the above definition is that we have hidden the data member variables inside a class and have also specified the access modifiers so that they are not accessible to the other classes. Thus encapsulation is also a kind of “data hiding” although later in the tutorial we will see that encapsulation is not the same as data hiding.

While abstraction is used to expose only the relevant details to the end-user, encapsulation mainly deals with data security. In ensuring data security, encapsulation shields the data members from unwanted access by specifying access modifiers and also bundles the data into a single unit.

So how can we define Encapsulation in Java?

 

Definition of Encapsulation

“Encapsulation in Java can be defined as a mechanism using which the data and the methods that work on that data are wrapped to form a single unit.”

So what we conclude from the above definition is that we have hidden the data member variables inside a class and have also specified the access modifiers so that they are not accessible to the other classes. Thus encapsulation is also a kind of “data hiding” although later in the tutorial we will see that encapsulation is not the same as data hiding. Visit the Edureify website for more information on certain topics.

 

Encapsulation

The above figure represents a class which is an encapsulation unit that bundles the data and methods operating on this data into a single unit. As encapsulation mainly deals with data, it is alternatively called “Data encapsulation”.

We can visualize encapsulation as a medical capsule. As we all know the medicine is enclosed inside a medical capsule. Similarly, data and methods are enclosed in a single unit in encapsulation.

Thus encapsulation acts as a protective shield around the data and prevents the data from unauthorized access by the outside world. In other words, it protects the sensitive data of our application

  • Use the access modifier ‘private’ to declare the class member variables.
  • To access these private member variables and change their values, we have to provide the public getter and setter methods respectively.

Java Encapsulation Example

//Student_Id and name bundled in a unit “Student” => encapsulation


class Student {

private int Student_Id;

private String name;

//getters, setters for Student_Id and name fields.

public int getId() {

return Student_Id;

}

public void setId(int s_id) {

this.Student_Id = s_id;

}

public String get the name() {

return name;

}

public void username(String s_name) {

this. name = s_name;

}

}

class Main{

public static void main(String[] args) {

//create an object for the Student class

Student s=new Student();

//set fields values using setter methods

s.setId (27);

s.setname("Tom Lee");

//print values using getter methods

System. out.println("Student Data:" + "\nStudent ID:" + s.getId()

+ " Student Name:" + s.getname());

}

}

In the above program, we declare a class which is the encapsulation unit. In this class, the Student has bundled the data (Student_Id and name) and the methods to read and set values for these members into a single unit.

Note the access modifiers associated with the member fields. Both the member fields are private so they are not accessible outside the Student class.

We provide getters (getId and get the name) to read the values of these fields and setter methods (setId and username) to set values for these methods. This is the only access they have and that also should be done using the Student class object. Visit the Edureify website for more concept-based introductions on certain topics.

Getter And Setter Methods

To implement encapsulation in Java, we make the data member variables of the class private. Now, these private variables are not accessible to anything outside the class including the class object. Getter and Setters are public methods that we can use to create, modify, delete, or simply view the values of the private variables.

Why Do We Need Encapsulation

There are various reasons why encapsulation is essential in Java:

  • Encapsulation allows us to modify the code or A part of the code without having to change any other functions or code.
  • Encapsulation controls how we access data.
  • We can modify the code based on the requirements using encapsulation.
  • Encapsulation makes our applications simpler.

Students can also read Edureify’s articles on Blockchain, Bootstrap, HTML, JavaScript, PHP, SQL, Node.js, and more to improve their technical skills for a better future.

Frequently Asked Questions

Q 1) Why Is Encapsulation used in Java?

Answer: Encapsulation in Java is mostly useful to hide the data. Or in other words, to decide about the access given to data as to who can access it, and who cannot

Q 2) What is Encapsulation in OOP?

Answer: Encapsulation is one of the important pillars of Object-oriented programming language and it deals with the bundling of data and methods operating on that data into a single unit. For example, a class in Java is an encapsulated structure. Encapsulation also deals with decisions regarding providing access to data.

Q 3) What is the advantage of Encapsulation in Java?

Answer: The major advantage of encapsulation in Java is data hiding. Using encapsulation we can allow the programmer to decide on the access to data and methods operating on that data. For example, if we want a particular piece of data to be inaccessible to anyone outside the class, then we make that data private.

Q 4) What is the Encapsulation process?

Answer: Encapsulation is a process of picking up data from one format or protocol (in networking terms) and translating or reformatting it into another format or protocol so that the data is accessible across the applications or network and at the same time it is protected.

Q 5) What is the last step in data encapsulation?

Answer: The last step in encapsulation is changing the user information into equivalent data. Then this data is changed into segments that are further transformed into data packets. Data packets are placed into a logical framework that can be transferred to and fro in the software environment.

Stay tuned to Edureify to learn about more web development tools.

 

Facebook Comments