Home > DeveloperSection > Blogs > Encapsulation in C# and OOPs

Encapsulation in C# and OOPs

C# C# 
0 Comment(s)
 2001  View(s)
Rate this:

Encapsulation in C# and OOPs

In This blog I am trying to explore the concept of Encapsulation in C#.


Encapsulation is the process of binding data 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 methodology, prevents access to implementation details.

Abstraction and encapsulation are related features in object oriented programming. Abstraction allows making relevant information 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 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)



            Customer c = new Customer("ram", 1000);

            c.createNewAccount("amit", 2000);



// Bank b = new Bank();// this line of code is invisible due to

//encapsulation(i.e information hiding

//were hided to class progaram)



    }//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    

Don't want to miss updates? Please click the below button!

Follow MindStick