DEV Community

Cover image for OOPS:Encapsulation in Java
Kathirvel S
Kathirvel S

Posted on

OOPS:Encapsulation in Java

I apreciate your interest to know about

"Encapsulation in java"

so you clicked this blog to know something about it

I sure that ,at the end of this blog you have some idea about this

without getting delay

let's start


Before Know about This

Before Dive into Encapsulation you have to Know about

Access Modifiers in java

You Go through it on Other resources

(or )

Kindly check out my previous blog for better understand

Here's the link --->

https://dev.to/kathirvel-s/access-modifiers-in-java-the-protection-powerhouse-103m


Let's BreakDown the Name of it

Let's break the word

En Capsule ation

Now Some people got a idea behind by this breakdown

if u not got

let understand clearly

I have searched about it on internet

i have got these paras

  • The term "encapsulation" comes from the Latin word capsula (meaning "a small box" or "container"),
  • combined with the prefix en- (meaning "in"). Literally, it means "to put something inside a capsule"

Ref link


Let's Start To Encapsulation In Java

Definition:

Encapsulation in Java is an object-oriented principle that binds data and methods into a single unit, typically a class. It restricts direct access to data by hiding implementation details. This ensures controlled interaction with the data through defined methods.

  • Achieved using access modifiers like private, protected, and public.
  • Improves data security by allowing validation through getters and setters.
  • Enhances code maintainability by isolating changes within the class.

look at this image:

think of it like a setting methods and data inside the capsule

How Encapsulation is Achieved in Java:

Encapsulation in Java is achieved using:

  • Private data members

  • Public getter and setter methods

Key Rules:

  • Declare data as private: Hide the class data so it cannot be accessed directly from outside the class.
  • Use getters and setters: Keep variables private and provide public getter and setter methods for controlled access and safe modification, often with validation.
  • Apply proper access modifiers: Use private for data hiding and public for methods that provide access.
class Programmer {

    private String name;

    // Getter method used to get the data
    public String getName() { return name; }

    // Setter method is used to set or modify the data
    public void setName(String name) {

        this.name = name;
    }
}


public class Another {

    public static void main(String[] args){

        Programmer p = new Programmer();
        p.setName("Kathir");
        System.out.println("Name=> " + p.getName());
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Name=> Kathir
Enter fullscreen mode Exit fullscreen mode

Explanation:

In the above example, we use the encapsulation and use getter (getName) and setter (setName) method which are used to show and modify the private data. This encapsulation mechanism protects the internal state of the Programmer object and allows for better control and flexibility in how the name attribute is accessed and modified.


Advantages of Encapsulation

The advantages of encapsulation are listed below:

Data Hiding: Encapsulation restricts direct access to class variables, protecting sensitive data from unauthorized access.
Improved Maintainability: Changes to internal implementation can be made without affecting external code that uses the class.
Enhanced Security: Encapsulation allows validation and control over data, preventing invalid or harmful values from being set.
Code Reusability: Encapsulated classes can be reused in different programs without exposing internal logic.
Better Modularity: Encapsulation promotes organized, modular code by keeping data and methods together within a class.


Disadvantages of Encapsulation

The disadvantages of encapsulation are listed below:

  • Increased Code Complexity: Writing getter and setter methods for every variable can make the code longer and slightly more complex.
  • Performance Overhead: Accessing data through methods instead of directly can introduce a minor performance cost, especially in performance-critical applications.
  • Less Flexibility in Some Cases: Over-restricting access to class members may limit the ability of other classes to extend or use the class efficiently.

Data Hiding vs Encapsulation

to make more clear idea about data hiding vs encapsulation

See this table


For clarity:

Try to answer these questions

1. What is encapsulation in Java?

A Wrapping data and methods into a single unit

B Allowing multiple forms of a method

C Inheriting properties from a parent class

D Overriding methods from an interface

2. Which benefit does encapsulation provide in Java?

A Increased code reusability

B Improved performance

C Improved code readability

D Improved data security and integrity

3. What is the key difference between Encapsulation and Abstraction in Java?

A Encapsulation is about hiding the details, Abstraction is about showing only essential features.

B Encapsulation is about showing only essential features, Abstraction is about hiding the details.

C Encapsulation uses classes, Abstraction uses interfaces

D There is no difference between Encapsulation and Abstraction.

4. Which of the following best represents encapsulation?

A Inheriting methods from a parent class

B Wrapping data and methods together and restricting direct access

C Using super to call parent methods

D Overriding methods in a child class


Takeaways:

Hope u got a clear idea about this concept

Think a encapsulation like a Sharing the data in capsule like a box or container

and authorized people can modifing data

and some datas never change


Reference:

Encapsulation word breakdown:

https://www.youtube.com/watch?v=s1-j3RKDvzg

Encapsualtion in java:

https://www.geeksforgeeks.org/java/encapsulation-in-java/

for better in depth understanding refer this blog:

https://www.scoutapm.com/blog/what-is-encapsulation

thank you for reading this blog

Practice these

and

Code repeat

Top comments (0)