---
title: "Encapsulation"  
description: "Encapsulation is the ability to hide its data and methods from outside the world and only expose data and methods that are required. Encapsulation is"  
author: "Anonymous User"  
published: 2012-09-11  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/363/encapsulation  
category: ".net"  
tags: [".net"]  
reading_time: 2 minutes  

---

# Encapsulation

[Encapsulation](https://www.mindstick.com/articles/12229/encapsulation-and-access-specifier) is the ability to hide its data and methods from outside the world and only expose data and methods that are required. Encapsulation is the technique or [process of making](https://www.mindstick.com/forum/159280/describe-the-process-of-making-an-api-call-from-a-react-frontend-to-a-node-js-backend) the fields in a class private and providing access to the fields using public methods. We can implement Encapsulation using Assessors.\

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone [outside the class](https://www.mindstick.com/interview/2468/can-you-access-the-private-method-from-outside-the-class), thereby hiding the fields within the class. For this reason, encapsulation is also [referred to as](https://answers.mindstick.com/qa/39930/at-the-turn-of-the-century-widespread-panic-occurred-that-at-midnight-all-computers-would-reset-to-zero-and-life-as-we-knew-it-would-cease-to-exist-what-was-this-doomsday-theory-commonly-referred-to-as) [data hiding](https://www.mindstick.com/forum/33865/what-is-data-hiding).

##### Type Member Access Modifiers:

We use [access modifiers](https://www.mindstick.com/articles/338838/explain-access-modifiers-in-java) such [as private](https://www.mindstick.com/interview/2711/what-if-the-main-method-is-declared-as-private), public, protected. It provides a way to [protect data](https://answers.mindstick.com/qa/114258/how-are-the-newest-trends-in-cybersecurity-helping-businesses-protect-data-from-emerging-threats). An access modifier allows you to specify the visibility.

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Encapsulation{    class EncapTest    {        private String custId; //private members        private String name;  //private members        private int age;     //private members        private int openingBalance;  //private members        public String getCustID()  //Creating Public methods to manipulating private data        {            return custId;        }        public int getCustAge()        {            return age;        }        public String getCustName()        {            return name;        }        public int getCustOpeBal()        {            return openingBalance;        }        public void setCustId(String newId)        {            custId = newId;        }        public void setCustAge(int newAge)        {            age = newAge;        }        public void setCustName(String newName)        {            name = newName;        }        public void setOpeBal(int newBal)        {            openingBalance = newBal;        }    }    class Program    {               static void Main(string[] args)        {            EncapTest encap = new EncapTest();            encap.setCustName("Ramesh"); //setting the value in private member through public methods            encap.setCustAge(20);            encap.setCustId("123");            encap.setOpeBal(5000);            Console.WriteLine("ID : " + encap.getCustID() + "\n" + "Name : " + encap.getCustName() + "\n" + "Age : " + encap.getCustAge() + "\n" + "Opening Balance : " + encap.getCustOpeBal());        }    }}
```

The public methods are the access points to this class's fields from the outside class. Normally these methods are referred as [getters and setters](https://www.mindstick.com/forum/160071/what-are-getters-and-setters-in-javascript-classes-and-why-are-they-useful). Therefore any class that wants to access the [variables](https://www.mindstick.com/articles/715/php-variables) should access them through these getters and setters.

##### Output:-

ID : 123

Name : Ramesh

Age : 20

Opening Balance : 5000

---

Original Source: https://www.mindstick.com/blog/363/encapsulation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
