Encapsulation & Abstraction in OOPs

Encapsulation is a fundamental concept in object-oriented programming (OOP) that involves bundling data and the methods that operate on that data within a single unit, known as a class.

Encapsulation & Abstraction in OOPs
Encapsulation,Abstraction in OOPs - thetechfoyer.thetechfoyer.com

This Articles helps you to understand the concept of Encapsulation, Abstraction in OOPs.

Encapsulation helps to protect the data and methods from outside interference, as it restricts direct access to them. In other words, encapsulation involves wrapping data and methods within a class to create a protective barrier around them.

Types of Encapsulation in OOPs

1. Data Member Encapsulation:  Data members can be defined as private members of the Class. Setters and Getters methods should be used by any object that wants to change or retrieve the value of a data member.

2. Method Encapsulation:  We can hide methods used for internal implementation that does not need to be visible to the public. Such method should be declared as private so that the user does not have access to it.

3. Class Encapsulation:  Our implementation might contain an internal implementation of a class that stores and processes specific information. We encapsulate that class by defining it as private and hiding it from user access. These classes should not be included in any public interface.

Encapsulation vs Abstraction
In abstraction, we focus on the outside view of an object and separate essential behavior from its implementation. Basic difference are:-
1. Abstraction is the process of exposing observable behavior of an object, while encapsulation is the process of hiding implementation that gives rise to that behavior.
2. Abstraction addresses problems at the interface level, while encapsulation addresses them at the implementation level.
3. Abstraction involves hiding unwanted information, while encapsulation involves protecting information from the outside by enclosing it within a single entity.
4. Abstraction can be implemented using abstract classes or interfaces, while encapsulation can be implemented using access modifiers like private, protected, and public.
5. Encapsulation creates explicit barriers between different abstractions and promotes clear separation of concerns.
Summary: An abstraction consists of how an object and its behaviors are presented to the user (An Interface), Where as encapsulation is a methodology that helps the programmer create this interface!

Advantages of Encapsulation
1. Increased code reusability: Encapsulation allows us to define a class once and create multiple objects from that class. By providing public, well-defined methods that act as the object's interface, we can make it easier for other programmers to understand object's role and reuse object's code in other applications.
2. Increased robustness:  Encapsulation reduces system complexity by allowing programmers to limit interdependencies between software components. This can help to increase robustness of the system by reducing number of potential points of failure. 3. Improved code maintenance: Encapsulation allows us to make code changes independently without affecting other classes. This can make it easier to maintain and update code over time.
4. Data security and information hiding: By making an object's data members and methods private, we can protect object's internal state and ensure that object is used in a consistent and predictable way. This can help to improve security of object's data and prevent it from being accessed or modified in an unauthorized way.
5. Improved code clarity and comprehensibility:  Encapsulation helps to keep related data members and methods together in a single class, making it easier to understand and work with the code. This can help to improve the clarity and comprehensibility of the code, making it easier to unit-test and debug.