|
| 1 | +### Encapsulation in Java |
| 2 | + |
| 3 | +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. |
| 4 | +encapsulation in java |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +The Java Bean class is the example of a fully encapsulated class. |
| 9 | + |
| 10 | +##### Advantage of Encapsulation in Java |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +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. |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +The encapsulate class is easy to test. So, it is better for unit testing. |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +##### Simple Example of Encapsulation in Java |
| 23 | +Student.java |
| 24 | +```java |
| 25 | + //A Java class which is a fully encapsulated class. |
| 26 | + //It has a private data member and getter and setter methods. |
| 27 | + package com.javatpoint; |
| 28 | + public class Student{ |
| 29 | + //private data member |
| 30 | + private String name; |
| 31 | + //getter method for name |
| 32 | + public String getName(){ |
| 33 | + return name; |
| 34 | + } |
| 35 | + //setter method for name |
| 36 | + public void setName(String name){ |
| 37 | + this.name=name |
| 38 | + } |
| 39 | + } |
| 40 | +``` |
| 41 | +Test.java |
| 42 | +```java |
| 43 | +package com.javatpoint; |
| 44 | +class Test{ |
| 45 | +public static void main(String[] args){ |
| 46 | +//creating instance of the encapsulated class |
| 47 | +Student s=new Student(); |
| 48 | +//setting value in the name member |
| 49 | +s.setName("vijay"); |
| 50 | +//getting value of the name member |
| 51 | +System.out.println(s.getName()); |
| 52 | +} |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +Output |
| 57 | +``` |
| 58 | +vijay |
| 59 | +``` |
| 60 | + |
| 61 | +---------- |
| 62 | + |
| 63 | +##### Read-Only class |
| 64 | +```java |
| 65 | +//A Java class which has only getter methods. |
| 66 | +public class Student{ |
| 67 | +//private data member |
| 68 | +private String college="AKG"; |
| 69 | +//getter method for college |
| 70 | +public String getCollege(){ |
| 71 | +return college; |
| 72 | +} |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +##### Write-Only class |
| 77 | + |
| 78 | +```java |
| 79 | + //A Java class which has only setter methods. |
| 80 | + public class Student{ |
| 81 | + //private data member |
| 82 | + private String college; |
| 83 | + //getter method for college |
| 84 | + public void setCollege(String college){ |
| 85 | + this.college=college; |
| 86 | + } |
| 87 | + } |
| 88 | +``` |
0 commit comments