Skip to content

Commit 3849e38

Browse files
Access modifiers, getters & setters
1 parent a1d71a9 commit 3849e38

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

4.Access_Modifiers/README.md

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Java Tutorial: Access modifiers, getters & setters in Java
2+
3+
### Access Modifiers
4+
- Access Modifiers specify where a property/method is accessible. There are four types of access modifiers in java :
5+
1. private
6+
2. default
7+
3. protected
8+
4. public
9+
10+
| **Access Modifier** | **within class** | **within package** | **outside package by subclass only** | **outside package** |
11+
|:---------------------:|:------------------:|:---------------------:|:---------------------------------------:|:----------------------:|
12+
| **public** | Y | Y | Y | Y |
13+
|**protected** | Y | Y | Y | N |
14+
|**Default** | Y | Y | N | N |
15+
|**private** | Y | N | N | N |
16+
17+
- From the above table, notice that the private access modifier can only be accessed within the class. So, let's try to access private modifiers outside the class :
18+
19+
```
20+
class Employee {
21+
22+
private int id;
23+
private String name;
24+
25+
}
26+
27+
public class CWH {
28+
public static void main(String[] args) {
29+
Employee emp1 = new Employee();
30+
emp1.id = 3;
31+
emp1.name = "Shubham";
32+
33+
}
34+
}
35+
```
36+
```
37+
Output :
38+
java: id has private access in Employee
39+
```
40+
41+
- You can see that the above code produces an error that we're trying to access a private variable outside the class. So, is there any way by which we can access the private access modifiers outside the class? The answer is Yes! We can access the private access modifiers outside the class with the help of getters and setters.
42+
43+
### Getters and Setters :
44+
- Getter ➼ Returns the value [accessors]
45+
- setter ➼ Sets / updates the value [mutators]
46+
- In the below code, we've created total 4 methods:
47+
1. setName(): The argument passed to this method is assigned to the private variable name.
48+
2. getName(): The method returns the value set by the setName() method.
49+
3. setId(): The integer argument passed to this method is assigned to the private variable id.
50+
4. getId): This method returns the value set by the setId() method.
51+
52+
```
53+
class Employee {
54+
55+
private int id;
56+
private String name;
57+
58+
public String getName(){
59+
return name;
60+
}
61+
public void setName(String n){
62+
name = n;
63+
}
64+
public void setId(int i){
65+
id = i;
66+
}
67+
public int getId(){
68+
return id;
69+
}
70+
}
71+
72+
public class CWH {
73+
public static void main(String[] args) {
74+
Employee emp1 = new Employee();
75+
76+
emp1.setName("Shubham");
77+
System.out.println(emp1.getName());
78+
emp1.setId(1);
79+
System.out.println(emp1.getId());
80+
81+
}
82+
}
83+
```
84+
```
85+
Output :
86+
Shubham
87+
1
88+
```
89+
90+
- As you can see that we've got our expected output. So, that's how we use the getters and setters method to get and set the values of private access modifiers outside the class.
91+
92+
### Code as described in the video:
93+
94+
```
95+
class MyEmployee{
96+
private int id;
97+
private String name;
98+
99+
public String getName(){
100+
return name;
101+
}
102+
public void setName(String n){
103+
this.name = n;
104+
}
105+
public void setId(int i){
106+
this.id = i;
107+
}
108+
public int getId(){
109+
return id;
110+
}
111+
}
112+
public class cwh_40_ch9 {
113+
public static void main(String[] args) {
114+
MyEmployee harry = new MyEmployee();
115+
// harry.id = 45;
116+
// harry.name = "CodeWithHarry"; --> Throws an error due to private access modifier
117+
harry.setName("CodeWithHarry");
118+
System.out.println(harry.getName());
119+
harry.setId(234);
120+
System.out.println(harry.getId());
121+
}
122+
}
123+
```
124+
125+
**Quick Quiz:** use the getters and setters from the main method
126+
127+
**Handwritten Notes: [Click to Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-40/Chapter9.pdf)**
128+
129+
**Ultimate Java Cheatsheet: [Click To Download](https://api.codewithharry.com/media/videoSeriesFiles/courseFiles/java-tutorials-for-beginners-40/UltimateJavaCheatSheet.pdf)**

0 commit comments

Comments
 (0)