---
title: "Encapsulation in java"  
description: "In this article I am telling you about Encapsulation in java"  
author: "Manoj Pandey"  
published: 2015-03-31  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1711/encapsulation-in-java  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# Encapsulation in java

[Encapsulation](https://www.mindstick.com/articles/12229/encapsulation-and-access-specifier) in java is a process of wrapping code and data together into a single unit or Encapsulation is the packing of data and [functions](https://www.mindstick.com/forum/34086/jquery-callback-functions) into a single component.\

Encapsulation is also [known as](https://answers.mindstick.com/qa/38482/name-the-world-s-lightest-material-which-was-manufactured-by-isro-scientists-at-the-vikram-sarabhai-space-centre-thiruvananthapuram-and-is-also-known-as-blue-air) “[data hiding](https://www.mindstick.com/forum/33865/what-is-data-hiding)”. For example a CPU of computer is mixed of several parts.

If a data member is private it means it can only be accessed within the same class. No outside class can [access private](https://www.mindstick.com/forum/12939/access-private-method-another-class-using-constructor-in-java) data member ([variable](https://www.mindstick.com/blog/11493/what-is-a-variable)) of other class.

The Java Bean class is the example of fully encapsulated class.

##### Advantages of Encapsulation -:

##### \
1. It improve maintainability, flexibility and re –usability. In encapsulation we have

##### need two method getter and setter. For example in a class we have two methods

##### () and setData (String data).

Example of class

```
    Class demo   {       private String mdata;       public String getData()         {           return mdata;          }Public void setData(String data)      {         Mdata=data;     }    }
```

In the above code the [implementation](https://answers.mindstick.com/qa/44913/who-is-the-minister-of-statistics-and-programme-implementation) code of void setData (String data) and String getData () can be changed at any point of time. Hence the code can be maintained at any point of time without breaking the classes that uses the code. This improves the re-[usability](https://www.mindstick.com/interview/886/are-there-usability-issues-with-ajax) of the underlying class.

2. Fields can be made read only or write only. For example we have fields which can’t change then we can set in private variable and remove setter method for set data can only make getter.

3. It hides implementation for example you are calling setData(String data) and you don’t know what is happening behind the setData methods.

##### Here I am creating an example of encapsulation

```
public class Student {          public static void main(String args[]) {           // Constructor of encapsulated class           MyEncasulatedClass myclass = new MyEncasulatedClass();           // set data in constructor class           myclass.setStudentId(1001);           myclass.setStudentName("Zack Parker");           myclass.setStudentCourse("MBA");            // get data in constructor class   System.out.println("Student Id-:     " + myclass.getStudentId());  System.out.println("Student Name-:   " + myclass.getStudentName());  System.out.print("Student Course-:  " + myclass.getStudentCourse());      }} // Encapsulated classclass MyEncasulatedClass {     // add private variable     private int sid;     private String s_name;     private String scourse;      // add getter and setter method     public void setStudentId(int id) {           sid = id;     }      public int getStudentId() {           return sid;     }      public void setStudentName(String name) {           s_name = name;     }      public String getStudentName() {           return s_name;     }      public void setStudentCourse(String course) {           scourse = course;     }      public String getStudentCourse() {           return scourse;     }} 
```

Now run [your application](https://answers.mindstick.com/qa/95487/how-do-you-troubleshoot-when-your-application-link-is-not-responding)

![Encapsulation in java](https://www.mindstick.com/mindstickarticle/465553d4-2a23-4622-b34e-9367c28395ea/images/3c1789a1-4e87-47ae-b8c4-4d9e0f180153.png)

---

Original Source: https://www.mindstick.com/articles/1711/encapsulation-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
