-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncapsulation.java
More file actions
55 lines (46 loc) · 1010 Bytes
/
Encapsulation.java
File metadata and controls
55 lines (46 loc) · 1010 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
*In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
*/
public class Encapsulation
{
public static void main(String[] args)
{
ABC abc = new ABC();
abc.setName("Bikramjeet Singh");
abc.setCity("Cambridge, ON");
abc.setAge(23);
System.out.println("My name is " + abc.getName() + ".");
System.out.println("I am from " + abc.getCity() + ".");
System.out.println("I am " + abc.getAge() + " years old.");
}
}
class ABC
{
private String name;
private String city;
private int age;
public void setName(String name)
{
this.name = name;
}
public void setCity(String city)
{
this.city = city;
}
public void setAge(int age)
{
this.age = age;
}
public String getName()
{
return name;
}
public String getCity()
{
return city;
}
public int getAge()
{
return age;
}
}