link: OOP

Encapsulation

Overview

Encapsulation is a fundamental concept in object-oriented programming that involves bundling the data (attributes) and methods that operate on the data into a single unit or class. It also restricts direct access to some of the object’s components, which can prevent the accidental modification of data and hide complex details.

Encapsulation helps to protect the internal state of an object and enforces controlled interactions through methods, which can be seen as a protective barrier that prevents the data from being accessed directly. The public interface of a class is what the outside world interacts with, while the implementation details are kept hidden.

Content

Example

To start a car engine, you only need to turn a key or press a button. You don’t need to connect wires under the hood, rotate the crankshaft and cylinders, and initiate the power cycle of the engine. These details are hidden under the hood of the car. You have only a simple interface: a start switch, a steering wheel and some pedals. This illustrates how each object has an interface—a public part of an object, open to interactions with other objects.

Key Concepts

  • Private and Protected Access: Encapsulation is achieved using access modifiers like private and protected. Private members are only accessible within the same class, while protected members are accessible in the class and its subclasses.
  • Simplified User Interface: Similar to using a car, where you start the engine with a key or button without needing to understand its internal mechanics, encapsulation provides a simplified interface to interact with.

Practical Example

In programming, interfaces and abstract classes are used to define a strict contract for what a class can do without dictating how it should do it. For instance, a FlyingTransport interface might declare a method like fly(origin, destination, passengers). This allows different transportation classes like Airplane, Helicopter, or even a DomesticatedGryphon to implement this interface in their own way while ensuring they can all be used interchangeably in contexts like an air transportation simulator.

csharp

public interface IFlyingTransport {
   void Fly(string origin, string destination, int passengers);
}
 
public class Airplane : IFlyingTransport {
   public void Fly(string origin, string destination, int passengers) {
       // Specific implementation for an airplane
   }
}
 
public class Helicopter : IFlyingTransport {
   public void Fly(string origin, string destination, int passengers) {
       // Specific implementation for a helicopter
   }
}
 
// Example of using encapsulation with an interface
public class Airport {
   public void ManageFlight(IFlyingTransport flyingTransport) {
       flyingTransport.Fly("New York", "London", 150);
   }
}
 

In this example, the Airport class is designed to operate with any objects that implement the IFlyingTransport interface. It doesn’t need to know the specific details of how each flying object operates, just that they can fly from one point to another. This design allows the implementation details of each flying object to remain encapsulated and interchangeable.

You could change the implementation of the fly method in these classes in any way you want. As long as the signature of the method remains the same as declared in the interface, all instances of the Airport class can work with your flying objects just fine.

Abstraction vs Encapsulation

Abstraction vs Encapsulation

Lets take a look on difference between Abstraction vs Encapsulation

A lot of times programmers often confuse abstraction with encapsulation because in reality the two concepts are quite intertwined and share a relationship between them. Abstraction, as we’ve seen pertains to hiding underlying details and implementation in a program. Encapsulation, on the other hand, describes how abstraction occurs in a program.

Abstraction is a design-level process but encapsulation is an implementation process. Encapsulation tells us how exactly you can implement abstraction in the program. Abstraction pertains to only displaying the essential details to the user whereas encapsulation pertains to typing up all the data members and associated member functions into a single abstracted unit.

NoAbstractionEncapsulation
1.It is the process of gaining information.It is a method that helps wrap up data into a single module.
2.The problems in this technique are solved at the interface level.Problems in encapsulation are solved at the implementation level.
3.It helps hide the unwanted details/information.It helps hide data using a single entity, or using a unit with the help of method that helps protect the information.
4.It can be implemented using abstract classes and interfaces.It can be implemented using access modifiers like public, private and protected.
5.The complexities of the implementation are hidden using interface and abstract class.The data is hidden using methods such as getters and setters.
6.Abstraction can be performed using objects that are encapsulated within a single module.Objects in encapsulation don’t need to be in abstraction.

Important

The most significant difference between the two is that data abstraction is a method which helps to hide the unwanted data from the user, while data encapsulation is a method which helps to hide data using a single entity.

Link to original

Summary

Summary

Encapsulation improves maintainability and flexibility of the code by separating the interface from the implementation. It enables objects to hide their state and to be manipulated only through their interface, reducing the likelihood of bugs and design errors while enhancing security and ease of use.