Encapsulation

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. 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. 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.

Let's see the simple example of encapsulation that has only one field with its setter and getter methods. Following code snippet is the simple Example of Encapsulation in Java.

File: Student.java


\begin{lstlisting}
//A Java class which is a fully encapsulated class.
//It has...
...me
public void setName(String name){
this.name=name
}
}
\end{lstlisting}

File: Test.java


\begin{lstlisting}
//A Java class to test the encapsulated class.
package com.j...
...e of the name member
System.out.println(s.getName());
}
}
\end{lstlisting}

Compile By: javac -d . Test.java; Run By: java com.javatpoint.Test. Output for the above program is going to be vijay.

Read-Only class:


\begin{lstlisting}
//A Java class which has only getter methods.
public class S...
...college
public String getCollege(){
return college;
}
}
\end{lstlisting}

Now, you can't change the value of the college data member which is AKG.


\begin{lstlisting}
s.setCollege(''KITE'');//will render compile time error
\end{lstlisting}

Write-Only class:


\begin{lstlisting}
//A Java class which has only setter methods.
public class S...
...oid setCollege(String college){
this.college=college;
}
}
\end{lstlisting}

Now, you can't get the value of the college, you can only change the value of college data member.


\begin{lstlisting}
System.out.println(s.getCollege());//Compile Time Error, beca...
...is private.
//So, it can't be accessed from outside the class
\end{lstlisting}

Another Example of Encapsulation in Java:

Let's see another example of encapsulation that has only four fields with its setter and getter methods.

File: Account.java


\begin{lstlisting}
//A Account class which is a fully encapsulated class.
//It ...
... void setAmount(float amount) {
this.amount = amount;
}
}
\end{lstlisting}

File: TestAccount.java


\begin{lstlisting}
//A Java class to test the encapsulated class Account.
publi...
....getName()+'' ''+acc.getEmail()+'' ''+acc.getAmount());
}
}
\end{lstlisting}

Output:


\begin{lstlisting}
7560504000 Sonoo Jaiswal sonoojaiswal@javatpoint.com 500000.0
\end{lstlisting}