-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncapsulation.java
More file actions
41 lines (31 loc) · 857 Bytes
/
Encapsulation.java
File metadata and controls
41 lines (31 loc) · 857 Bytes
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
class Human{
// int age; // bu default its public
// String name;
// lets make it private
private int age = 20;
private String name = "faisal";
// Encapsulation - only methods on this class should access this class variables
public void setAge(int a){
age = a;
}
public void setName(String n){
name = n;
}
public int getAge(){
return age;
}
public String getName(){
return name;
}
}
public class Encapsulation{
public static void main(String args[]){
Human obj = new Human();
// obj.age = 11;
// obj.name="Faisal";
// System.out.println(obj.name);
obj.setAge(20);
obj.setName("Human");
System.out.println(obj.getName() + " " + obj.getAge());
}
}