Search code examples
javaencapsulation

Java: Encapsulation Concept


In Java we're always following Encapsulation rules, but there is one paradox in my mind, in all documents the concept of Encapsulation is defined like this:

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class.

We're defining all the variables inside the classes as private to not getting accessed by anyone outside the class.

Again we are creating setters and getters for those private variables, and those variables are getting accessible for anyone outside the class by using setters and getters.

I will appreciate if anyone can help to get clear the concept of Encapsulation for me.

Aren't we just increasing code by declaring private and creating setters and getters?


Solution

  • Ref: https://www.javatpoint.com/encapsulation

    Encapsulation in Java

    Encapsulation in Java is a process of wrapping code and data together into a single unit, for example, a capsule which is mixed of several medicines.

    encapsulation in java We can create a fully encapsulated class in Java by making all the data members of the class private. Now we can use setter and getter methods to set and get the data in it.

    The Java Bean class is the example of a fully encapsulated class.

    Advantage of Encapsulation in Java

    By providing only a setter or getter method, you can make the class read-only or write-only. In other words, you can skip the getter or setter methods.

    It provides you the control over the data. Suppose you want to set the value of id which should be greater than 100 only, you can write the logic inside the setter method. You can write the logic not to store the negative numbers in the setter methods.

    It is a way to achieve data hiding in Java because other class will not be able to access the data through the private data members.

    The encapsulate class is easy to test. So, it is better for unit testing.

    The standard IDE's are providing the facility to generate the getters and setters. So, it is easy and fast to create an encapsulated class in Java.