1// java program on encapsulation
2class EncapsulationExample
3{
4 private int ID;
5 private String stuName;
6 private int stuAge;
7 // getter and setter methods
8 public int getStudentID()
9 {
10 return ID;
11 }
12 public String getStudentName()
13 {
14 return stuName;
15 }
16 public int getStudentAge()
17 {
18 return stuAge;
19 }
20 public void setStudentAge(int number)
21 {
22 stuAge = number;
23 }
24 public void setStudentName(String number)
25 {
26 stuName = number;
27 }
28 public void setStudentID(int number)
29 {
30 ID = number;
31 }
32}
33public class ExampleForEncapsulation
34{
35 public static void main(String[] args)
36 {
37 EncapsulationExample student = new EncapsulationExample();
38 student.setStudentName("Virat");
39 student.setStudentAge(5);
40 student.setStudentID(2353);
41 System.out.println("Student Name: " + student.getStudentName());
42 System.out.println("Student ID: " + student.getStudentID());
43 System.out.println("Student Age: " + student.getStudentAge());
44 }
45}
11-ENCAPSULATION: We can hide direct access to data by using
2private key and we can access private data by using getter and
3setter method.