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.

Author: Gurunatha Dogi

Gurunatha Dogi

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 and lots more..... read more

Comments

64x64
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.
64x64
By Anjana on 2016-04-30
its very useful to understand the concepts in a simple way.
64x64
By Imran on 2016-02-12
very nicely explained really appreciated.....:)
64x64
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?
64x64
By Balu on 2015-08-24
Great Explanation
64x64
By Hamza Zulfiqar on 2015-05-27
Please tell me the basic concepts of structure programing and oop and difference between them with examples.
64x64
By Bhim on 2015-04-05
thanks sir...your concept's so clear and everyone can understand..
64x64
By Swapnil aher on 2015-02-19
Thanks for the explanation. It's clear and succinct !
64x64
By Ramendra kumar on 2015-02-10
nice explain..... but abstraction and abstract is same or not
64x64
By Amer on 2014-11-25
simple, useful and easy to understand, thanks.
64x64
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 ?
64x64
By Pakiarajp on 2014-09-08
i need full oops concept with example
64x64
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..
64x64
By Ravindra Ganpat jitkar on 2014-07-10
very good answer in normal language.
64x64
By Rajdeep Sil on 2014-06-21
By using which keyword,we can achieved encapsulation and also abstraction?
64x64
By Himanshu saini on 2014-05-22
it's a really helpful answer
64x64
By ABDUL on 2014-05-07
excellent :) explanation about encapsulation now my concept are very much clear thanks bro
64x64
By Shweta on 2014-04-24
Great explanation .. now my concept about encapsulation are cleared . thnx dude..
64x64
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
64x64
By Rashedul Islam on 2014-03-23
Really nice example. Thanks a lot.
64x64
By Maheshbabuummaneni on 2013-10-22
it's nice example and good usefull for me thnnks u friend.
64x64
By Ravindra on 2013-10-17
Nice explanation . thank you very much
64x64
By Rakesh on 2013-10-10
Nice Article.
64x64
By Bindu on 2013-09-19
thank u...... it is very comfort to understand
64x64
By Lokeshprc on 2013-09-19
simply good
64x64
By User on 2013-09-12
simple and clear explanation.. thanks.. keep up...
64x64
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.
64x64
By Sambasiva rao on 2013-08-13
its good....
64x64
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...
64x64
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..
64x64
By Tshidiso on 2013-07-23
Thanks i understand better now
64x64
By Hemant on 2013-06-23
great and simple explanation

Add a Comment

Types of Access Modifiers in C#