Encapsulation in Java
Encapsulation in Java
Encapsulation is defined as the wrapping up of data under a single unit. It is the mechanism that binds
together code and the data it manipulates.Other way to think about encapsulation is, it is a protective
shield that prevents the data from being accessed by the code outside this shield.
Technically in encapsulation, the variables or data of a class is hidden from any other class and
can be accessed only through any member function of own class in which they are declared.
As in encapsulation, the data in a class is hidden from other classes, so it is also known as data-
hiding.
Encapsulation can be achieved by: Declaring all the variables in the class as private and writing
public methods in the class to set and get the values of variables.
Geek's age: 19
Geek's roll: 51
Advantages of Encapsulation:
Data Hiding: The user will have no idea about the inner implementation of the class. It will not
be visible to the user that how the class is storing values in the variables. He only knows that we
are passing the values to a setter method and variables are getting initialized with that value.
Increased Flexibility: We can make the variables of the class as read-only or write-only
depending on our requirement. If we wish to make the variables as read-only then we have to omit
the setter methods like setName(), setAge() etc. from the above program or if we wish to make the
variables as write-only then we have to omit the get methods like getName(), getAge() etc. from the
above program
Reusability: Encapsulation also improves the re-usability and easy to change with new
requirements.
Testing code is easy: Encapsulated code is easy to test for unit testing.
This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute,
you can also write an article using contribute.geeksforgeeks.orgor mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help
other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the
topic discussed above.
------------------------------------------------------------------------------------------------------------------------------------------
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.
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. public class Student{
4. //private data member
5. private String name;
6. //getter method for name
7. public String getName(){
8. return name;
9. }
10. //setter method for name
11. public void setName(String name){
12. this.name=name
13. }
14. }
File: Test.java
1. //A Java class to test the encapsulated class.
2. class Test{
3. public static void main(String[] args){
4. //creating instance of the encapsulated class
5. Student s=new Student();
6. //setting value in the name member
7. s.setName("vijay");
8. //getting value of the name member
9. System.out.println(s.getName());
10. }
11. }
Output:
vijay
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("sonoojaiswal@SSCT.com");
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. }
Test it Now
Output:
1. class Student{
2. int rollno;
3. String name;
4. float fee;
5. Student(int rollno,String name,float fee){
6. rollno=rollno;
7. name=name;
8. fee=fee;
9. }
10. void display(){System.out.println(rollno+" "+name+" "+fee);}
11. }
12. class TestThis1{
13. public static void main(String args[]){
14. Student s1=new Student(111,"ankit",5000f);
15. Student s2=new Student(112,"sumit",6000f);
16. s1.display();
17. s2.display();
18. }}
Test it Now
Output:
0 null 0.0
0 null 0.0
In the above example, parameters (formal arguments) and instance variables are same. So,
we are using this keyword to distinguish local variable and instance variable.
Output:
If local variables(formal arguments) and instance variables are different, there is no need to
use this keyword like in the following program:
Output:
1. class A{
2. void m(){System.out.println("hello m");}
3. void n(){
4. System.out.println("hello n");
5. //m();//same as this.m()
6. this.m();
7. }
8. }
9. class TestThis4{
10. public static void main(String args[]){
11. A a=new A();
12. a.n();
13. }}
Test it Now
Output:
hello n
hello m
1. class A{
2. A(){System.out.println("hello a");}
3. A(int x){
4. this();
5. System.out.println(x);
6. }
7. }
8. class TestThis5{
9. public static void main(String args[]){
10. A a=new A(10);
11. }}
Test it Now
Output:
hello a
10
1. class A{
2. A(){
3. this(5);
4. System.out.println("hello a");
5. }
6. A(int x){
7. System.out.println(x);
8. }
9. }
10. class TestThis6{
11. public static void main(String args[]){
12. A a=new A();
13. }}
Test it Now
Output:
5
hello a
1. class Student{
2. int rollno;
3. String name,course;
4. float fee;
5. Student(int rollno,String name,String course){
6. this.rollno=rollno;
7. this.name=name;
8. this.course=course;
9. }
10. Student(int rollno,String name,String course,float fee){
11. this(rollno,name,course);//reusing constructor
12. this.fee=fee;
13. }
14. void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);}
15. }
16. class TestThis7{
17. public static void main(String args[]){
18. Student s1=new Student(111,"ankit","java");
19. Student s2=new Student(112,"sumit","java",6000f);
20. s1.display();
21. s2.display();
22. }}
Test it Now
Output:
1. class Student{
2. int rollno;
3. String name,course;
4. float fee;
5. Student(int rollno,String name,String course){
6. this.rollno=rollno;
7. this.name=name;
8. this.course=course;
9. }
10. Student(int rollno,String name,String course,float fee){
11. this.fee=fee;
12. this(rollno,name,course);//C.T.Error
13. }
14. void display(){System.out.println(rollno+" "+name+" "+course+" "+fee);}
15. }
16. class TestThis8{
17. public static void main(String args[]){
18. Student s1=new Student(111,"ankit","java");
19. Student s2=new Student(112,"sumit","java",6000f);
20. s1.display();
21. s2.display();
22. }}
Test it Now
Compile Time Error: Call to this must be first statement in constructor