OOPs | Encapsulation
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. This concept 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.
How to Hide Information via Encapsulation?
Use Access Modifiers to control visibility and accessibility of class-level structures and hide sensitive data from users.
Programmers should use these access modifiers to differentiate between public and non-public interface of an object.
The public access modifier has a broad scope. It implies that public class members (classes, methods, or data members) can be accessed using object of other classes. In other words, public class members have no restriction on the scope, and they can be accessible from everywhere in the program.
The class members declared private are limited to the scope of the class and can be accessed only by the member methods inside the class. In other words, they can not be accessed directly by any object or method outside the class.
A protected access modifier is similar to a private access modifier, but access level is limited to the same class or any subclass inherited from that class. This access through inheritance can access base class elements in the derived class depending on the mode of inheritance.
When no access modifier is specified for a class, method, or data member, it is said to have the default access modifier. In other words, the default members access is limited to the current or the same package.
Classes not in the same package cannot access or use the default members.
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.