-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetandset.java
More file actions
48 lines (24 loc) · 733 Bytes
/
getandset.java
File metadata and controls
48 lines (24 loc) · 733 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//Getter's and setter's with encapsulation example
public class Car{
private String name ;
private int price ;
public void setName(String carname){
name = carname;
}
public String getName(){
return name;
}
public void setPrice(int price){
this.price = price; //this refers to class member
}
public int getPrice(){
return price;
}
public static void main(String[] args){
Car c = new Car();
c.setName("BMW");
c.setPrice(10000);
System.out.println("Car name:"+c.getName());
System.out.println("Car price:"+c.getPrice());
}
}