Home » Tutorials » C# » Difference between Abstraction and Encapsulation in C#

Difference between Abstraction and Encapsulation in C#

Use of Private Constructor in C# with an example
Scheduler for multiple instances in C#

Difference between Abstraction and Encapsulation in C#

Hey folks, till day you have read Use of Private Constructor in C# with an exampleDifference between Throw and Throw ex in C#Difference between Var and Dynamic in c#,Difference Between WCF and WebAPIDifference between String and string in C#Difference between Nullable<> and ?,Difference between Hashtable and Dictionary,Insert data into Database table using SQLBulkCopy class in C#Ping in C# , Call method after specific time interval C#Insert Retrieve images from Database in asp.net LINQ TO SQL and Compare two objects in C# . Today we will discuss regarding Difference between Abstraction and Encapsulation in C#.

There are four pillars of OOP:

  1. Encapsulation
  2. Abstraction
  3. Inheritance
  4. Polymorphism

Among of these four properties or features, we will discuss Abstraction and Encapsulation in this post.

Abstraction:

  1. Abstraction is “To represent the essential feature without representing the background details.”
  2.  Abstraction lets you focus on what the object does instead of how it does it.
  3.  Abstraction provides you a generalized view of your classes or objects by providing relevant information.
  4. Abstraction is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.

Real-world Example of Abstraction:

Suppose you have 3 mobile phones as in the following:

  1. Mobile1 (Features: Calling, SMS)
  2. Mobile2 (Features: Calling, SMS, FM Radio, MP3, Camera)
  3. Mobile3 (Features:Calling, SMS, FM Radio, MP3, Camera, Video Recording, Reading E-mails)

Abstract information (necessary and common information) for the object “Mobile Phone” is that it makes a call to any number and can send SMS.

So that, for a mobile phone object you will have the abstract class as in the following:

 

Encapsulation:

  1. Wrapping up a data member and a method together into a single unit (in other words class) is called Encapsulation.
  2. Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.
  3. Encapsulation means hiding the internal details of an object, in other words how an object works.
  4. Encapsulation prevents clients from seeing its inside view, where the behavior of the abstraction is implemented.
  5. Encapsulation is a technique used to protect the information in an object from another object.
  6. Hide the data for security such as making the variables private, and expose the property to access the private data that will be public.
  7. Encapsulation is like you can play music in either of mobile. It means this is the property of encapsulating members and functions.

     

Real-world Example of Encapsulation:
Let’s use as an example Mobile Phones and Mobile Phone Manufacturers.
Suppose you are a Mobile Phone Manufacturer and you have designed and developed a Mobile Phone design (a class). Now by using machinery you are manufacturing Mobile Phones (objects) for selling, when you sell your Mobile Phone the user only learns how to use the Mobile Phone but not how the Mobile Phone works internally means how does mobile internally handle calling function and what will happen to connect call etc.

This means that you are creating the class with functions and by with objects (capsules) of which you are making available the functionality of your class by that object and without the interference in the original class.

Compare Difference between Abstraction and Encapsulation:

 

Abstraction Encapsulation
 The problem to be solvd in the design level. Solves the problem in the implementation level.
Used to hide the unwanted data and only give relevant data. Hide the code & data to protect the data from outer world.
Focus on the object instead of how does it work. Encapsulation means hiding the internal details or mechanics of how an object does something.
Abstraction is outer layout in terms of design. Encapsulation is inner layout in terms of implementation.

Hope now you have cleared confusion or difference between Abstraction and Encapsulation. If you liked this post, do share with your friends and colleagues and subscribe for all latest posts.

About

Hello friends, My self Dhruv Sheth, I have more than 6 years of Industrial experience as a Software Developer. In my career I got chance to work with asp.net, c#,Web API, JavaScript, JQuery, WPF, WCF , Windows Application, PostScript, vb.net, GhostScript etc. So I have decided to share my knowledge with everyone and finally I have come up with TechnoThirsty.

Leave a Reply

Your email address will not be published. Required fields are marked *

*
*

Time limit is exhausted. Please reload CAPTCHA.




DISCLAIMER:
The content is copyrighted to technothirsty.com and may not be reproduced on other websites without permission from the owner.You may contact us using the information below.