-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAccessTest.java
More file actions
37 lines (27 loc) · 736 Bytes
/
AccessTest.java
File metadata and controls
37 lines (27 loc) · 736 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
package chapter7;
/*This program demonstrates the difference between public and private*/
class Test5 {
int a;
public int b;
private int c;
//methods to access c
void setc(int i) {
c = i; //Sets c's value
}
int getc() {
return c; //Gets c's value
}
}
public class AccessTest {
public static void main(String[] args) {
Test5 ob = new Test5();
//These are ok a and b can be accessed directly
ob.a = 10;
ob.b = 20;
//This is not ok , will cause an error.
// ob.c=100;
//You must access c through its methods.
ob.setc(100);
System.out.println("a, b and c :" + ob.a + " " + ob.b + " " + ob.getc());
}
}