forked from Nabin-joshi/java_notes_and_programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encapsulaton.java
43 lines (33 loc) · 925 Bytes
/
encapsulaton.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Human{
// encapsulation is done with access specifier
// every time you create a instance variable make it private
private int age;
private String name;
// private variables can only be accessed through method only mainly getter setter
// initially private variable is for that particular class
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
// int age;
// String name;
}
public class encapsulaton {
public static void main(String[] args) {
Human obj =new Human();
obj.setAge(12);
obj.setName("Nabin");
System.out.println(obj.getAge() +" : "+obj.getName());
// obj.age=12;
// obj.name="Nabin";
// System.out.println(obj.age);
}
}