---
title: "What is encapsulation? and why is use the encapsulation technique in OOPs?"  
description: "What is encapsulation? and why is use the encapsulation technique in OOPs?"  
author: "Ravi Vishwakarma"  
published: 2021-08-23  
updated: 2023-04-10  
canonical: https://www.mindstick.com/forum/156704/what-is-encapsulation-and-why-is-use-the-encapsulation-technique-in-oops  
category: "c#"  
tags: ["c#", "java"]  
reading_time: 4 minutes  

---

# What is encapsulation? and why is use the encapsulation technique in OOPs?

What is [encapsulation](https://www.mindstick.com/articles/12229/encapsulation-and-access-specifier)? and why is use the encapsulation [technique](https://answers.mindstick.com/qa/93922/why-c-sharp-programmers-use-properties-technique-in-c-sharp-programming) in [OOPs](https://www.mindstick.com/articles/1558/introduction-to-oops-object-oriented-programming-system)?

## Replies

### Reply by Aryan Kumar

Encapsulation is an important concept in object-oriented programming (OOP) that involves hiding the inner workings of an object from the outside world. This is done by restricting access to the object's data and methods through a well-defined interface.

In OOP, encapsulation is achieved through the use of classes that encapsulate data and methods. These classes act as blueprints for creating objects, each with their own unique set of data and methods.

Encapsulation is used in OOP to improve software design by reducing complexity and improving maintainability. By hiding the implementation details of an object, changes can be made to the implementation without affecting other parts of the program that use the object.

In addition to improving software design, encapsulation also helps to improve security by preventing unauthorized access to an object's data. This helps to ensure that the object behaves correctly and that the data is not accidentally or intentionally modified in unexpected ways.

Overall, encapsulation is an essential technique in modern software development that helps to improve software design, reduce complexity, and improve security.

```java
public class Person {
   private String name;
   private int age;

   public Person(String name, int age) {
       this.name = name;
       this.age = age;
   }

   public String getName() {
       return name;
   }

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

   public int getAge() {
       return age;
   }

   public void setAge(int age) {
       this.age = age;
   }
}
public class Main {
   public static void main(String[] args) {
       Person person = new Person("John", 25);
       System.out.println("Name: " + person.getName());
       System.out.println("Age: " + person.getAge());

       person.setName("Jane");
       person.setAge(30);

       System.out.println("Name: " + person.getName());
       System.out.println("Age: " + person.getAge());
   }
}
```

In the **Main** class, we create a **Person** object and use the getter and setter methods to access and modify the object's data. This demonstrates how encapsulation can be used to hide the implementation details of an object and provide a well-defined interface for accessing and modifying its data.

### Reply by Ashutosh Kumar Verma

## Encapsulation -

Encapsulation is used to bind different methods and variables into a single units. A class is the best example to show the encapsulation technique which contain member methods and variables into a single class.

**Use of Encapsulation -** Encapsulation is used to protect the data from outer function which can change or remove of essential data or code. if we define a class with its properties (i.e. get and set methods), then we can change into the data but it not being changed into original data of that class.

**Ex.**

```
namespace MyConsole
{
    class EncapsulationTest
    {
        private string name;
        private string address;
        public string Name
        {
            set {
                name = value;
            }
            get {
                return name;
            }
        }
        public string Location
        {
            set {
                address = value;
            }
            get {
                return address;
            }
        }
        static void Main(string[] arg)
        {
            EncapsulationTest capsule = new EncapsulationTest();
            capsule.Name = 'Ashu';
            capsule.Location = 'Vns';
            Console.WriteLine('Name is  '+capsule.Name);
            Console.WriteLine('Location is  '+capsule.Location);
        }    }
}
```

Output- Name is Ashu

Location is Vns

\


---

Original Source: https://www.mindstick.com/forum/156704/what-is-encapsulation-and-why-is-use-the-encapsulation-technique-in-oops

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
