Skip to content

Commit 2de7475

Browse files
Access modifiers, getters & setters
1 parent 5b76a95 commit 2de7475

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

40.Access_Modifiers/CWH.java

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Employee {
2+
3+
private int id;
4+
private String name;
5+
6+
}
7+
8+
public class CWH {
9+
public static void main(String[] args) {
10+
Employee emp1 = new Employee();
11+
emp1.id = 3;
12+
emp1.name = "Shubham";
13+
14+
}
15+
}

40.Access_Modifiers/CWH1.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Employee {
2+
3+
private int id;
4+
private String name;
5+
6+
public String getName(){
7+
return name;
8+
}
9+
public void setName(String n){
10+
name = n;
11+
}
12+
public void setId(int i){
13+
id = i;
14+
}
15+
public int getId(){
16+
return id;
17+
}
18+
}
19+
20+
public class CWH {
21+
public static void main(String[] args) {
22+
Employee emp1 = new Employee();
23+
24+
emp1.setName("Shubham");
25+
System.out.println(emp1.getName());
26+
emp1.setId(1);
27+
System.out.println(emp1.getId());
28+
29+
}
30+
}

40.Access_Modifiers/Chapter9.pdf

1.63 MB
Binary file not shown.

40.Access_Modifiers/cwh_40_ch9.java

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class MyEmployee{
2+
private int id;
3+
private String name;
4+
5+
public String getName(){
6+
return name;
7+
}
8+
public void setName(String n){
9+
this.name = n;
10+
}
11+
public void setId(int i){
12+
this.id = i;
13+
}
14+
public int getId(){
15+
return id;
16+
}
17+
}
18+
public class cwh_40_ch9 {
19+
public static void main(String[] args) {
20+
MyEmployee harry = new MyEmployee();
21+
// harry.id = 45;
22+
// harry.name = "CodeWithHarry"; --> Throws an error due to private access modifier
23+
harry.setName("CodeWithHarry");
24+
System.out.println(harry.getName());
25+
harry.setId(234);
26+
System.out.println(harry.getId());
27+
}
28+
}

0 commit comments

Comments
 (0)