Encapsulation in Java
Encapsulation in Java
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.
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.
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.
File: Student.java
1. //A Java class which is a fully encapsulated class.
2. //It has a private data member and getter and setter methods.
3. package com.javatpoint;
4. public class Student{
5. //private data member
6. private String name;
7. //getter method for name
8. public String getName(){
9. return name;
10. }
11. //setter method for name
12. public void setName(String name){
13. this.name=name
14. }
15. }
File: Test.java
1. //A Java class to test the encapsulated class.
2. package com.javatpoint;
3. class Test{
4. public static void main(String[] args){
5. //creating instance of the encapsulated class
6. Student s=new Student();
7. //setting value in the name member
8. s.setName("vijay");
9. //getting value of the name member
10. System.out.println(s.getName());
11. }
12. }
Compile By: javac -d . Test.java
Run By: java com.javatpoint.Test
Output:
vijay
Read-Only class
1. //A Java class which has only getter methods.
2. public class Student{
3. //private data member
4. private String college="AKG";
5. //getter method for college
6. public String getCollege(){
7. return college;
8. }
9. }
Now, you can't change the value of the college data member which is "AKG".
1. s.setCollege("KITE");//will render compile time error
Write-Only class
1. //A Java class which has only setter methods.
2. public class Student{
3. //private data member
4. private String college;
5. //getter method for college
6. public void setCollege(String college){
7. this.college=college;
8. }
9. }
Now, you can't get the value of the college, you can only change the value of college
data member.
1. System.out.println(s.getCollege());//Compile Time Error, because there is no such metho
d
2. System.out.println(s.college);//Compile Time Error, because the college data member is
private.
3. //So, it can't be accessed from outside the class
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
1. //A Account class which is a fully encapsulated class.
2. //It has a private data member and getter and setter methods.
3. class Account {
4. //private data members
5. private long acc_no;
6. private String name,email;
7. private float amount;
8. //public getter and setter methods
9. public long getAcc_no() {
10. return acc_no;
11. }
12. public void setAcc_no(long acc_no) {
13. this.acc_no = acc_no;
14. }
15. public String getName() {
16. return name;
17. }
18. public void setName(String name) {
19. this.name = name;
20. }
21. public String getEmail() {
22. return email;
23. }
24. public void setEmail(String email) {
25. this.email = email;
26. }
27. public float getAmount() {
28. return amount;
29. }
30. public void setAmount(float amount) {
31. this.amount = amount;
32. }
33.
34. }
File: TestAccount.java
1. //A Java class to test the encapsulated class Account.
2. public class TestEncapsulation {
3. public static void main(String[] args) {
4. //creating instance of Account class
5. Account acc=new Account();
6. //setting values through setter methods
7. acc.setAcc_no(7560504000L);
8. acc.setName("Sonoo Jaiswal");
9. acc.setEmail("[email protected]");
10. acc.setAmount(500000f);
11. //getting values through getter methods
12. System.out.println(acc.getAcc_no()+" "+acc.getName()+" "+acc.getEmail()+"
"+acc.getAmount());
13. }
14. }
Abstraction Vs Encapsulation
Java is an object-oriented programming language and it follows OOPs concepts. The
OOPs concepts include classes, objects, polymorphism, inheritance. There are two other
features of OOPs i.e. abstraction and encapsulation. They both seem very similar but
totally different in concept and implementation. The major difference between
abstraction and encapsulation is that abstraction hides the code complexity while
encapsulation hides the internal working from the outside world. In this section, we will
discuss abstraction and encapsulation and the differences between abstraction and
encapsulation in Java.
Abstraction
It is a feature of OOPs. It is used to hide the unnecessary information or data from the
user but shows the essential data that is useful for the user. It can be achieved by using
the interface and the abstract class. In interfaces, only the methods are exposed to the
end-user. The best example of abstraction is a TV remote. The user only interacts with
the outer interface that is nothing but keys. The user only knows which key to press for
what function.
1. //abstract class
2. abstract class Shape
3. {
4. //abstract method
5. //note that we have not implemented the functionality of the method
6. public abstract void draw();
7. }
8. class Circle extends Shape
9. {
10. //implementing functionality of the abstract method
11. public void draw()
12. {
13. System.out.println("Circle!");
14. }
15. }
16. //main class
17. public class Test
18. {
19. public static void main(String[] args)
20. {
21. Shape circle = new Circle();
22. //invoking abstract method draw()
23. circle.draw();
24. }
25. }
Encapsulation
It is also a feature of OOP. It is used to bind up the data into a single unit called class. It
provides the mechanism which is known as data hiding. It is an important feature of
OOPs. It prevents to access data members from the outside of the class. It is also
necessary from the security point of view.
EncapsulationDemo.java
1. //A Java class to test the encapsulated class Account
2. public class EncapsulationDemo
3. {
4. public static void main(String[] args)
5. {
6. //creating instance of Account class
7. Account acc=new Account();
8. //setting values through setter methods
9. acc.setAcc_no(7560504000L);
10. acc.setName("Mark Dennis");
11. acc.setEmail("[email protected]");
12. acc.setAmount(500000f);
13. //getting values through getter methods
14. System.out.println(acc.getAcc_no()+" "+acc.getName()+" "+acc.getEmail()+"
"+acc.getAmount());
15. }
16. }
Account.java
1. class Customer
2. {
3. //private data members
4. private long cstmr_id;
5. private String cstmr_name, cstmr_email;
6. private float amount;
7. //public getter and setter methods
8. public long getAcc_no()
9. {
10. return acc_no;
11. }
12. public void setAcc_no(long acc_no)
13. {
14. this.acc_no = acc_no;
15. }
16. public String getName()
17. {
18. return name;
19. }
20. public void setName(String name)
21. {
22. this.name = name;
23. }
24. public String getEmail()
25. {
26. return email;
27. }
28. public void setEmail(String email)
29. {
30. this.email = email;
31. }
32. public float getAmount()
33. {
34. return amount;
35. }
36. public void setAmount(float amount)
37. {
38. this.amount = amount;
39. }
40. }
Abstraction is a feature of OOPs that hides Encapsulation is also a feature of OOPs. It hides the code an
the unnecessary detail but shows the data into a single entity or unit so that the data can b
essential information. protected from the outside world.
The objects are encapsulated that helps to The object need not to abstract that result in encapsulation.
perform abstraction.