Skip to content

Commit ab8d09e

Browse files
committed
updated readme
1 parent 51e687c commit ab8d09e

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

.idea/workspace.xml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ It is used to develop rich internet applications. It uses a light-weight user in
111111
|[Abstract](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Abstraction/abstractClass.md)|
112112
|[Interface](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Abstraction/Interface.md)|
113113
|[Difference Between Abstract and Interface](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Abstraction/abstractvsinterface.md)|
114-
|[]()|
115-
|[]()|
116-
|[]()|
117-
|[]()|
114+
|[Java Encapsulation](https://github.com/connectaman/Java_Notes_and_Programs/tree/master/src/Encapsulation)|
115+
|[Packages](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Encapsulation/Package.md)|
116+
|[Access Modifiers](https://github.com/connectaman/Java_Notes_and_Programs/blob/master/src/Encapsulation/AcessModifier.md)|
117+
|[Encapsulation]()|
118118
|[]()|
119119
|[]()|
120120
|[]()|

src/Encapsulation/Encapsulation.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)