---
title: "Encapsulation in C# and OOPs"  
description: "In This blog I am trying to explore the concept of Encapsulation in C#.EncapsulationEncapsulation is the process of binding data and method into a sin"  
author: "shreesh chandra shukla"  
published: 2013-07-15  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/551/encapsulation-in-c-sharp-and-oops  
category: "c#"  
tags: ["c#"]  
reading_time: 5 minutes  

---

# Encapsulation in C# and OOPs

In This blog I [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to [explore the concept](https://answers.mindstick.com/qa/112684/how-do-americans-explore-the-concept-of-hygge-and-coziness-in-their-homes) of Encapsulation in C#.

Encapsulation

Encapsulation is the process of [binding data](https://www.mindstick.com/forum/4/binding-data-with-datagridview) and method into a single unit (viz. class, structure, properties etc). This is the first step towards the object orientation technology, where the term object (instance of class) which contains the abstract members.

Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object [oriented programming](https://www.mindstick.com/forum/162/object-oriented-programming-oop-s) methodology, prevents access to [implementation](https://www.mindstick.com/forum/33764/how-to-implementation-of-class-in-c-sharp) details.

[Abstraction and encapsulation](https://www.mindstick.com/forum/156710/differentiate-between-data-abstraction-and-encapsulation) are related features in object oriented programming. Abstraction allows making [relevant information](https://answers.mindstick.com/qa/105781/how-to-interpret-financial-news-and-filter-relevant-information-for-investing) visible and encapsulation enables a programmer to implement the desired level of abstraction.

Encapsulation is implemented by using access specifier. An access specifier defines the scope and visibility of a class member.

##### C# supports the following access specifier:

1. Private

2. Public

3. Protected

4. Internal

5. Protected internal

##### Illustration of Encapsulation:

Now we take a simple example of encapsulation to explore the encapsulation process. Banking is very common [functionality](https://www.mindstick.com/blog/136/using-watermark-functionality-in-textbox-by-jquery) in today’s Environment. So we let’s take the banking process to explore the Encapsulation process by encapsulation (I.e hiding various members of class throw the desired level of abstraction using various access specifier defined by the c#).

```
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;
namespace EncpsulationExample{    class Program    {        static void Main(string[] args)        {            Console.Clear();            Customer c = new Customer("ram", 1000);            c.createNewAccount("amit", 2000);            //Console.WriteLine();            c.showbalance();
// Bank b = new Bank();// this line of code is invisible due to//encapsulation(i.e information hiding//were hided to class progaram)         Console.ReadKey();        }    }//close of class progarm    class Bank    { // class members with protected level abstarction,// which are only visible to the derived class of bank class        protected string acc_no;        protected string costomerName;        protected int amount;        protected string log_id;        protected string log_pass;        public Bank()// this enables the object of this class is possible        {            this.acc_no = string.Empty;            this.amount = 0;            this.costomerName = string.Empty;            this.log_id = string.Empty;            this.log_pass = string.Empty;        }        protected Bank(string name, int amt)//bank class object could'nt
//be created in any class//except to the base class        {            this.costomerName = name;            this.amount = amt;        }// opens new account thois is public any one can open account in bank        public void createNewAccount(string name, int amount)        {            Random random = new Random();//generating the random account number
   this.acc_no = name.Substring(0, 2) + (random.Next(100, 10000)).ToString();
    this.costomerName = name;    this.amount = amount;     this.log_id = name.Substring(0, 2) + (random.Next(10, 1000)).ToString();     this.log_pass = name.Substring(0, 4) + (random.Next(10, 1000)).ToString();
        }        public void deposite(int amount)// this method is pblic //othe person could deposite//money in othres account        {            this.amount -= amount;
        }
        private void withdrawal(int amount)// this is abstact at top level//abstraction in the hairachy of abstraction{ // because the withdrwal must protected//to auauthorised personal to reciev money from bank            this.amount += amount;        }        protected int balanceCheck()        {            return (this.amount);        }        private void closeAccount(string acc_no)// the bank have only authority
//to close any customers account        {            Console.WriteLine("thia account has successfully been closed");        }    }//close of class bank    class Customer : Bank // customenr class inherite the bank class now    { //all the member except to private// method withdrwal, is available/visible
        //to the coustomer class         int bal;        public Customer(string name, int amt)            : base(name, amt) //calling the base class cunstructot//by passing name and//amount as two parameter    {     bal = 0;    Console.WriteLine("hello i was called");        }        public void showbalance() // any one can make enquary for//available balance in particula account        {       bal = balanceCheck();      Console.WriteLine("the current account balance is RS:{0}", bal);        }
    } //close of class customer
} // namespace closed    
```

---

Original Source: https://www.mindstick.com/blog/551/encapsulation-in-c-sharp-and-oops

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
