1、protected的類、類屬變量及方法,包內(nèi)的任何類,及包外的那些繼承了此類的子類才能訪問;
注意:子類如處于不同的包,則相互間不能訪問繼承自父類的方法。
所有不能訪問的方法都已經(jīng)被注釋:
1
package packageA;
2
3
public class Base {
4
public String publicStr = "publicString";
5
protected String protectedStr = "protectedString";
6
String defaultStr = "defaultString";
7
private String privateStr = "privateString";
8
9
public void print() {
10
System.out.println("packageA.Base has access to");
11
System.out.println(" " + publicStr);
12
System.out.println(" " + protectedStr);
13
System.out.println(" " + defaultStr);
14
System.out.println(" " + privateStr);
15
16
Base b = new Base(); // -- other Base instance
17
System.out.println(" b." + b.publicStr);
18
System.out.println(" b." + b.protectedStr);
19
System.out.println(" b." + b.defaultStr);
20
System.out.println(" b." + b.privateStr);
21
}
22
}
23
24
--------------------------------------------------------------------------------
25
26
package packageA;
27
28
public class SubA extends Base {
29
public void print() {
30
System.out.println("packageA.SubA has access to");
31
System.out.println(" " + publicStr + " (inherited from Base)");
32
System.out.println(" " + protectedStr + " (inherited from Base)");
33
System.out.println(" " + defaultStr + " (inherited from Base)");
34
// -- not accessible - private elements are even not inherited
35
// System.out.println(privateStr);
36
37
Base b = new Base(); // -- other Base instance
38
System.out.println(" b." + b.publicStr);
39
System.out.println(" b." + b.protectedStr);
40
System.out.println(" b." + b.defaultStr);
41
// -- not accessible
42
// System.out.println(b.privateStr);
43
}
44
}
45
46
--------------------------------------------------------------------------------
47
48
package packageA;
49
50
public class AnotherA {
51
public void print() {
52
System.out.println("packageA.AnotherA has access to");
53
Base b = new Base();
54
System.out.println(" b." + b.publicStr);
55
System.out.println(" b." + b.protectedStr);
56
System.out.println(" b." + b.defaultStr);
57
// System.out.println(b.privateStr);
58
}
59
}
60
61
--------------------------------------------------------------------------------
62
63
package packageB;
64
import packageA.Base;
65
66
public class SubB extends Base {
67
public void print() {
68
System.out.println("packageB.SubB has access to");
69
System.out.println(" " + publicStr + " (inherited from Base)");
70
// -- protectedStr is inherited element -> accessible
71
System.out.println(" " + protectedStr + " (inherited from Base)");
72
// -- not accessible
73
// System.out.println(defaultStr);
74
// System.out.println(privateStr);
75
76
Base b = new Base(); // -- other Base instance
77
System.out.println(" b." + b.publicStr);
78
// -- protected element, which belongs to other object -> not accessible
79
// System.out.println(b.protectedStr);
80
81
// -- not accessible
82
// System.out.println(b.defaultStr);
83
// System.out.println(b.privateStr);
84
}
85
}
86
87
--------------------------------------------------------------------------------
88
89
package packageB;
90
import packageA.Base;
91
92
public class AnotherB {
93
public void print() {
94
System.out.println("packageB.AnotherB has access to");
95
Base b = new Base();
96
System.out.println(" b." + b.publicStr);
97
// -- not accessible
98
// System.out.println(b.protectedStr);
99
// System.out.println(b.defaultStr);
100
// System.out.println(b.privateStr);
101
}
102
}
103
104
--------------------------------------------------------------------------------
105
106
import packageA.*;
107
import packageB.*;
108
109
// -- testing class
110
public class TestProtection {
111
public static void main(String[] args) {
112
// -- all classes are public, so class TestProtection
113
// -- has access to all of them
114
new Base().print();
115
new SubA().print();
116
new AnotherA().print();
117
new SubB().print();
118
new AnotherB().print();
119
}
120
}
121

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

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121
