What is the Difference Between Encapsulation and Abstraction in C#

The main difference between Encapsulation and Abstraction in C# is that the encapsulation wraps the data and methods into a single unit while abstraction hides the implementation details and only displays the functionality to users.  

C# is a high-level, general-purpose programming language developed by Microsoft. It is built on the .NET framework. The framework provides the environment and the necessary tools to develop and deploy applications. One major advantage of C# is that it supports Object Oriented Programming (OOP). Therefore, the developers can easily model and represent the real world scenarios to solve the problems. Encapsulation and Abstraction are two major pillars of OOP.

Key Areas Covered

1. What is Encapsulation in C#
   – Definition, Functionality
2. What is Abstraction in C#
   – Definition, Functionality
3. Difference Between Encapsulation and Abstraction in C#
   – Comparison of Key Differences

Key Terms

Abstraction, C#, Encapsulation, .NET Framework, OOP

Difference Between Encapsulation and Abstraction in C# - Comparison Summary

What is Encapsulation in C#

Encapsulation connects data and methods into a single unit. For example, assume a medical capsule. The covering protects the medicine inside the capsule. Encapsulation is similar to that.

The main objective of encapsulation is to prevent accessing the data. There are several access modifiers available in C# such as public, protected, private, etc. The public members are accessible by any other class. The protected members are accessible within the class and by subclasses. However, private members are accessible only within the class. The programmer can make the data members private and use public getters and setters to read and write the data. Likewise, encapsulation combines data and methods to a single unit and also prevents accessing data directly.

Difference Between Encapsulation and Abstraction in C#

Figure 1: C# program with Encapsulation

In this above program, Student is a class. It has a private data member called name. The method setName receives a String and it assigns that value to name. The method getName returns the String name. In the main program, there is an object called s of type Student. In line 20, the setName is called on the object s. The String “Ann” is passed to that method. In line 21, the getName helps to obtain the name. Finally, the name prints on the console. Encapsulation combines the data and methods to a single unit. Rather than accessing the data members directly, the programmer uses the public set method to assign the value and public get method to obtain the value.

What is Abstraction in C#

Abstraction helps to hide the implementation details and allows displaying only the functionality to the user. Assume a remote controller of a television. The user does not need to know about the internal circuitry of the controller to use it. Abstraction is similar to that. It focuses on what the object does rather than paying attention to how it is done.

In C#, there are two methods to achieve abstraction. They are by using an abstract class or interface. An abstract class can have abstract and non-abstract methods. A class that extends an abstract class should provide the implementations for the abstract methods. In an interface, all the methods are abstract. A class that implements an interface should provide the definitions for those abstract methods.

Main Difference - Encapsulation in C# vs Abstraction in C#

Figure 2: C# Program with an Abstract class

In the above program, Shape is an abstract class. It has an abstract method called draw. Triangle class extends Shape. Thus, this class implements the draw method. In the main method, s is an object of type Shape. The draw method is called the draw method of Triangle.

Difference Between Encapsulation and Abstraction in C#_Figure 3

Figure 3: C# program with an Interface

In the above program, Shape is an interface. It has an abstract method called draw. Triangle class extends Shape. It provides the definition for the draw method. In the main method, s is an object of type Shape. The draw method is called the draw method of Triangle.

Difference Between Encapsulation and Abstraction in C#

Definition

Encapsulation in C# is an OOP concept that binds the data and methods together to create a single unit. Abstraction in C#, on the other hand, is an OOP concept that hides the implementation details and displays only the functionality to the user. Thus, this is the main difference between Encapsulation and Abstraction.

Method of achieving

Furthermore, the programmer can make the data members private and use public methods to achieve encapsulation in C#. There are abstract classes and interfaces to achieve abstraction in C#.

Usage

The usage is another difference between Encapsulation and Abstraction. That is; encapsulation hides data for the purpose of data protection while abstraction hides the implementation details to reduce code complexity.

Conclusion

Encapsulation and Abstraction are two OOP concepts that we can implement using C# programming. The main difference between encapsulation and abstraction in C# is that the encapsulation wraps the data and methods into a single unit while abstraction hides the implementation details and only displays the functionality to users.  

Reference:

1.“C# Abstract – Javatpoint.” Www.javatpoint.com, Available here.
2.“C# Interface – Javatpoint.” Www.javatpoint.com, Available here.
3.“C# Encapsulation – Javatpoint.” Www.javatpoint.com, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply