Encapsulation

What is encapsulation? Explain with a code. Why it is important or needed and how?

Encapsulation means binding our data with methods in a single unit /class only ways to access to those data is methods and nobody can outside directly access this variable

Benefits:

  1. Setter methods can include validation logic to ensure that any data being set meets certain criteria

    1. Example: check the amount > 500 in Bank Class deposit()

    2. Hides the implementation logics of validation

  2. Can set read-only or write-only access only to that variable

    1. Example: read-only access to accounNumber in Bank Class

  3. Hides implementation logics and rest of the code depends on the method only. you can change implementation logics any time any outside can't don't know about it.

    1. Example : withdraw() and deposit() function in Bank Classs

Encapsulation in Java

Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP) that refers to the bundling of data (variables) and methods (functions) into a single unit, typically a class and it restricts direct access to some of the object's components.

Why Encapsulation?

  • Access modifiers are mainly used for encapsulation. It can help us to control what part of a program can access the members of a class

  • It prevents outer classes from accessing and changing fields and methods of a class. This also helps to achieve data hiding.

  • Encapsulation refers to the bundling of related fields and methods together. This can be used to achieve data hiding. Encapsulation in itself is not data hiding.

  • Suppose age is private of Person class that means why cannot directly access it like p1.age = 24; for access it we need to the getter and setter method which only known by the developer it written the code and even we cannot know whether it is read-only or write-only or both: This called Data Hiding

  • The getter and setter methods provide read-only or write-only access to our class fields.

  • It helps to control the values of our data fields

Example Code:

Explanation:

  1. Private Variables: The variables name, age, and salary are declared as private, so they cannot be accessed directly from outside the class.

  2. Public Methods: Public getter and setter methods are provided to access and update the private variables. This allows for controlled access.

  3. Validation: Setter methods include validation logic to ensure the data remains consistent and valid.

  4. Main Class: In the Main class, we create an instance of EncapsulatedEmployee and access its properties using the public methods.

By using encapsulation, we ensure that the internal state of the EncapsulatedEmployee object is protected from unintended or harmful changes from outside the class, while still providing a way to access and modify it in a controlled manner.

If there is public get and set method, how it restricts access...? What the use case?

Understanding Encapsulation with Getters and Setters

The concept of encapsulation can be confusing when considering the use of public getters and setters. While getters and setters do provide access to private fields, they still play a crucial role in encapsulation. Here’s why and how they are useful:

Controlled Access

  1. Validation: Setters can include validation logic to ensure that only valid data is set. This prevents the internal state from being corrupted by invalid values.

  2. Read-Only or Write-Only Properties: By providing only a getter or a setter, you can make a field read-only or write-only.

  3. Encapsulation of Behavior: Getters and setters allow you to change the internal implementation without affecting external code. For example, you could change how a property is calculated internally without changing how it’s accessed.

Example Code with Controlled Access

Here's a more detailed example that showcases controlled access and validation:

Explanation

  1. Validation: The deposit and withdraw methods include validation to ensure that the operations are only performed with valid amounts.

  2. Read-Only Property: The account number is read-only; there is no setter for it.

  3. Controlled Access: The balance can only be modified through the deposit and withdraw methods, which ensures that the balance can never be set to an invalid state.

By using getters and setters, we can encapsulate the internal state and ensure that only valid operations are performed, thus maintaining the integrity of the object.

Last updated