Encapsulation in Java

In Java encapsulation is the technique which we come across every day. Encapsulation is one of the major pillars of OOP. Using encapsulation we can achieve great things. Today in this article we are going to discuss encapsulation. Let’s understand what encapsulation is.

What is Encapsulation?

Encapsulation is bundling similar data and methods inside a class. Encapsulation helps in data hiding also. Data hiding can be achieved using the access modifiers in java. There are four access modifiers in java

        1.      Public – visible from anywhere.

        2.      Private – visible only within the class.

        3.      Protected – visible only in a package and among the subclasses.

        4.      Default – visible within the package.

Using these access modifiers in java we can achieve data hiding using encapsulation. To achieve encapsulation in java we need to consider two things:

        1.      Use private access modifier with the variables of the class.

        2.      Provide public getter and setter in the class, so that data access will be allowed through these public methods only.

Why Encapsulation?

Mechanism of encapsulation provides many advantages, some of them are stated below:

        1.      Imagine the code without encapsulation. If we don’t know which data belongs to which class and where to keep all the data, the management of the data will become tedious. Encapsulation helps to remove this problem. Encapsulation improves the flexibility, maintainability and reusability of the data by keeping/encapsulating the similar kind of data.

        2.      The fields can be made read-only (if we don't define the set methods in the class) or write-only (if we don't define the get methods in the class). For example, if we have a field (or variable) that we don't want to change, we just define the variable as private and instead of setting and getting both we only need to get the method for that variable. Since the set method is not present, there is no way that an outer class can modify the value of that field.

        3.      The user would not know what happens behind the scenes. They would only know that to update a field call set method and read a field call get method, but what these get and set methods do is purely hidden from them. 

Before we move further do check out the book link given below in order to buy this best book in the market. The book covers every aspect of core java with latest version upgrade java 8.

                                                 

Example of Encapsulation:

Let’s discuss the example of Encapsulation by considering Student.java class. This class should contain the data which is related with student and should contain private data members and public getters and setters for each data member for access.

Output:

Student Id – 12

Student Name – Ram

Student Address - India


 


Related Articles:

        1.      Core java interview questions.

        2.      Java 8 interview questions.

        3.      Abstraction in Java.

        4.      Internal working of Hashmap in java.

        5.    Runtime Polymorphism in java.

        6.    Compile time polymorphism in java.

Post a Comment

0 Comments