Are you struggling to develop clean, efficient and maintainable code? The two key principles of Object-Oriented Programming (OOP), encapsulation and abstraction, can come to your aid. While one helps maintain privacy, the other offers simplification of complex systems. To help you with the Encapsulation vs Abstraction comparison, here’s a detailed guide.
Difference Between Encapsulation and Abstraction
The difference between data abstraction and encapsulation as per different parameters is as follows:
Parameter |
Encapsulation |
Abstraction |
Objective |
Wraps data and method to form a single unit |
Hide complex and unnecessary details except for the important part |
Usage |
To maintain privacy to avoid unauthorized access |
Decrease complexity by hiding the essential features |
Implementation |
Performed through access modifiers such as public, private and protected |
Done via abstract classes and interfaces |
Function |
Contains the information |
Gains the information |
Dependency |
Data is not necessary to be abstracted for encapsulation |
Data is encapsulated for abstraction |
Focus |
Involves ‘how’ object performs the action |
Involves ‘what’ action object performs |
Offers solution at |
Deals with issues at the implementation level |
Deals with issues at the design level |
What is Encapsulation?
The concept of encapsulation offers a method to prevent unauthorized access to the data and methods. The process is a feature of Object-Oriented Programming (OOP) that takes place by binding or grouping the data and method/variables to form a single unit, decreasing the visible part of the code.
It is performed through private variable declaration along with the inclusion of public methods for obtaining the values of variables. Encapsulation leads to limited access to data, which is possible via the member function of the class where they are declared. Hence, only authorized users can modify the data.
“Encapsulation is the bundling of data with the methods that operate on that data.”
– Herbert Schildt
Example of Encapsulation
Let’s say there is a class ‘Vehicle’ that encapsulates the internal and private details of how the vehicle works, which includes fuel level and engine state. The ‘Car’ and ‘Motorcycle’ are public methods that hold access to these details in a controlled manner. Here is the program in Java depicting the scenario:
// Base class Vehicle (encapsulating internal details) class Vehicle { // Private variables (encapsulation) private int fuelLevel; // Fuel level in percentage (0 to 100) private boolean engineState; // Engine state (true = running, false = stopped) // Constructor to initialize fuel level and engine state public Vehicle(int fuelLevel) { this.fuelLevel = fuelLevel; this.engineState = false; // Engine is off by default } // Public method to get fuel level public int getFuelLevel() { return fuelLevel; } // Public method to set fuel level public void setFuelLevel(int fuelAmount) { if (fuelAmount >= 0 && fuelAmount <= 100) { this.fuelLevel = fuelAmount; } else { System.out.println(“Invalid fuel level. Must be between 0 and 100.”); } } // Public method to start the engine
|
Source link