-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencap.java
More file actions
46 lines (39 loc) · 1.22 KB
/
encap.java
File metadata and controls
46 lines (39 loc) · 1.22 KB
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
// Demonstrating encapsulation in Java
class Student {
// Private fields to encapsulate the name and grade
private String name;
private int grade;
// Getter method for name
public String getName() {
return name;
}
// Setter method for name
public void setName(String name) {
this.name = name;
}
// Getter method for grade
public int getGrade() {
return grade;
}
// Setter method for grade
public void setGrade(int grade) {
if (grade >= 0 && grade <= 100) { // Adding a simple validation
this.grade = grade;
} else {
System.out.println("Grade must be between 0 and 100.");
}
}
}
// Driver class
class Encap {
public static void main(String[] args) {
// Creating an object of the Student class
Student student = new Student();
// Setting the name and grade using setter methods
student.setName("Vedik Tiwari");
student.setGrade(90);
// Getting the name and grade using getter methods
System.out.println("Name: " + student.getName());
System.out.println("Grade: " + student.getGrade());
}
}