-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccesmodifiers.java
More file actions
30 lines (27 loc) · 984 Bytes
/
accesmodifiers.java
File metadata and controls
30 lines (27 loc) · 984 Bytes
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
class MyEmployee{
private int id;
private String name;
public void setname(String n){
name =n;
}
public String getname(){
return name;
}
public void setid(int i){
id = i;
}
public int getid(){
return id;
}
}
public class accesmodifiers {
public static void main(String[] args) {
MyEmployee vanshi = new MyEmployee();
//vanshi.id = 1;//this will give an error because id is private and cannot be accessed directly
// vanshi.name = "Vanshi";//this will also give an error because name is private and cannot be accessed directly
vanshi.setid(56); //using the setter method to set the value of id
vanshi.setname("Vanshi"); //using the setter method to set the value of name
System.out.println(vanshi.getid()); //using the getter method to get the value of id
System.out.println(vanshi.getname()); //using the getter method to get the value of name
}
}