-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh09_40_AccessModifiers.java
More file actions
108 lines (88 loc) · 3.13 KB
/
Ch09_40_AccessModifiers.java
File metadata and controls
108 lines (88 loc) · 3.13 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
class schoolStudent{
int roll_no;
String name;
}
class schoolStudent1{
private int roll_no;
private String name;
// Setter
public void setName(String n) {
name = n;
}
public void setRoll_no(int rn) {
roll_no = rn;
}
// Getter
public String getName(){
return name;
}
public int getRoll_no(){
return roll_no;
}
}
class CircleProps{
private float radius;
private float perimeter;
private float area;
public void setRadius(int rad) {
radius = rad;
}
public void setPerimeter(float p) {
if (p == 6*radius){
perimeter = p;
System.out.println("Correct Perimeter Entered");
}
else {
System.out.println("Wrong Perimeter entered");
}
}
public void setArea(float a) {
if (a == 3*(radius*radius)){
area = a;
System.out.println("Correct Area Entered");
}
else {
System.out.println("Wrong area entered");
}
}
public float getRadius() {
return radius;
}
}
public class Ch09_40_AccessModifiers {
public static void main(String[] args) {
System.out.println();
/* Access Modifiers - specifies where an Attributes or a method is accessible.
That is whether if a file is inherited , will method from the earlier files be used or not.
These are of four types - 1. Private
2. Default
3. Protected
4. Public */
//Private:
schoolStudent Student1 = new schoolStudent();
Student1.name = "Vedant";
Student1.roll_no = 212096;
System.out.println("Name of Student is: " + Student1.name);
System.out.println("Roll No. of Student is: " + Student1.roll_no);
System.out.println();
// Note there are no error in this code but if we replicate with attributes being Private.
schoolStudent1 Student2 = new schoolStudent1();
//Student2.name = "Vedant"; // As the attributes are set to private
//Student2.roll_no = 212096; // They Cannot be accessed directly, we need to use Getters and Setters method.
//The use of Getter and Setters are that it helps the attributes to remain private and can only be accessed
// using methods hence no breaching of data.
// Shortcut to automatically make Getter and Setter is : Code>Generate>Getter and Setters
Student2.setName("Vedant");
Student2.setRoll_no(212096); // Data set without changing the original attributes
System.out.println("Student's name: " + Student2.getName());
System.out.println("Student's Roll No: " + Student2.getRoll_no()); // Data printed
System.out.println();
// ******** \\
// Quiz - Circle
CircleProps Circle = new CircleProps();
Circle.setRadius(6);
System.out.println("Radius of Circle: " + Circle.getRadius());
Circle.setPerimeter(36);
Circle.setArea(107);
}
}