Encapsulation in C# using an example

 

Encapsulation is way to hide data, properties and methods from outside the world or 
outside of the class scope and exposing only necessary thing.Encapsulation complements Abstraction. Abstraction display only important features of a class and Encapsulation hides unwanted data or private data from outside of a class.It hides the information within the object and prevents from accidental corruption.

How we can achieve Encapsulation

We can achieve Encapsulation by using "private" access modifier as shown in below snippet of code.

 

 
class Employee{

 private void AccountInformation(){
  Console.WriteLine("Displaying Account Details");
 }

}

Why to use Encapsulation

Encapsulation means protecting important data inside the class which we do not 
want to be exposed outside of the class.

Lets consider example of a Television(TV).

If you have seen important TV machine, TV connections and TV color tube is hidden inside the TV case which is not been exposed for viewers like us and exposed only neccessary things of a TV like TV Channel keys, TV volume keys, ON/OFF switch, Cable Switch and TV remote control for viewers to use it.This means TV machine, TV connections and TV color tube is an unwanted data and not needed for viewers to see is been hidden from outside the world.

So encapsulation means hiding the important features of a class which is not been needed to be exposed outside of a class and exposing only necessary things of a class.

Here hidden part of a class acts like Encapsulation and exposed part of a class acts like Abstraction.

For more information on Abstraction check out this article Abstraction in Csharp with an example.

Example of Encapsulation using Csharp

 
class clsTelevision{

  private void TVmachine(){
  
    Console.WriteLine("Machine of a Television");

  }

  private void TVcolortube(){
  
    Console.WriteLine("Color Tube of a Television");

  }

   public void TVKeys(){
  
    Console.WriteLine("Keys of a Television");

  }
   
  public void TVRemote(){
  
    Console.WriteLine("Remote of a Television");

  }

  public void TVScreen(){
  
    Console.WriteLine("Wide Screen of a Television");

  }

}

So as you can see from our above code that we have hidden core part of a television methods by using "private" access modifier and exposed only necessary methods for a viewers to use it using "public" access modifier.


So before implementing encapsulation or abstaction think in terms of real world scenario as needed for your application or list down things which you want hide or display in your applications.


For any doubts kindly let me know through your comments.

 

 

:: SHARE THIS POST ::

        
 

More C# Articles To Read Next:

 
Apr
30
2013

Explain the copy and private constructor with an example in C#

CLICK TO READ FULL ARTICLE

Views - 51842 |Category - C#

Apr
29
2013

Types of Access Modifiers in C#

CLICK TO READ FULL ARTICLE

Views - 22086 |Category - C#

Apr
24
2013

Difference Between Array and Array List in C#

CLICK TO READ FULL ARTICLE

Views - 41099 |Category - C#

Apr
22
2013

How to Create and Initialize Jagged Arrays using C#

CLICK TO READ FULL ARTICLE

Views - 19925 |Category - C#

Apr
20
2013

Try, Catch and Finally Blocks in C#

CLICK TO READ FULL ARTICLE

Views - 17021 |Category - C#

 
Author: Gurunatha Dogi
is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server, Cell Phones and lots more. Follow me @ and twitter.
Leave a Comment......

All fields marked with * are mandatory

ls7v7

 

 
Comments

Posted by Subhash on 2016-05-15

Hi sir i'm get confused by reading your Article on Abstraction and Encapsulation plz can you clear my confusion.

Posted by Anjana on 2016-04-30

its very useful to understand the concepts in a simple way.

Posted by Imran on 2016-02-12

very nicely explained really appreciated.....:)

Posted by Sasikala on 2015-12-10

It is really nice,easy to understand and good explanation.I need full concept in c sharp.can please send it to my mail id?

Posted by Balu on 2015-08-24

Great Explanation

Posted by Hamza Zulfiqar on 2015-05-27

Please tell me the basic concepts of structure programing and oop and difference between them with examples.

Posted by Bhim on 2015-04-05

thanks sir...your concept's so clear and everyone can understand..

Posted by Swapnil aher on 2015-02-19

Thanks for the explanation. It's clear and succinct !

Posted by Ramendra kumar on 2015-02-10

nice explain..... but abstraction and abstract is same or not

Posted by Amer on 2014-11-25

simple, useful and easy to understand, thanks.

Posted by Ali Hasna on 2014-09-11

hi, I have a Question if I private the method in the method declared the variable "A" and assign value to "A" if i use this method with in the class and print the value of "A" so it can be show in the output or not ?

Posted by Pakiarajp on 2014-09-08

i need full oops concept with example

Posted by Santhosh AHs on 2014-08-28

i have gone through all your articles in your blog...these are all super super and super article even small child can learn easily........all articles are super..

Posted by Ravindra Ganpat jitkar on 2014-07-10

very good answer in normal language.

Posted by Rajdeep Sil on 2014-06-21

By using which keyword,we can achieved encapsulation and also abstraction?

Posted by Himanshu saini on 2014-05-22

it's a really helpful answer

Posted by ABDUL on 2014-05-07

excellent :) explanation about encapsulation now my concept are very much clear thanks bro

Posted by Shweta on 2014-04-24

Great explanation .. now my concept about encapsulation are cleared . thnx dude..

Posted by Pankaj on 2014-04-08

Sir provide me some basically simple example of encapsulation in the example how will we hide the internal workings of an object's behavior and its data with the help of encapsulation plz provide me example of encapsulation

Posted by Rashedul Islam on 2014-03-23

Really nice example. Thanks a lot.

Posted by Maheshbabuummaneni on 2013-10-22

it's nice example and good usefull for me thnnks u friend.

Posted by Ravindra on 2013-10-17

Nice explanation . thank you very much

Posted by Rakesh on 2013-10-10

Nice Article.

Posted by Lokeshprc on 2013-09-19

simply good

Posted by Bindu on 2013-09-19

thank u...... it is very comfort to understand

Posted by User on 2013-09-12

simple and clear explanation.. thanks.. keep up...

Posted by Mohit Saxena on 2013-08-25

Both Encapsulation & Abstration Classes Looks like Same. So what is the Difference in Encapsulation & Abstration in classes look wise.

Posted by Sambasiva rao on 2013-08-13

its good....

Posted by Chandra on 2013-08-07

Please Explain same concepts in asp.net that means at what time in real time project we r using..

Posted by Chandra on 2013-08-07

This is articles are very nice but same thing how to use in asp.net real time..Please help me...

Posted by Tshidiso on 2013-07-23

Thanks i understand better now

Posted by Hemant on 2013-06-23

great and simple explanation