Java Reference
In-Depth Information
Let's look at an example. The following Phone class is declared in the com.sybex.demos
package and demonstrates members with each level of access:
1. package com.sybex.demos;
2.
3. public class Phone {
4. public int number;
5. int extension;
6. private String ringTone;
7.
8. public Phone(int n, int e) {
9. number = n;
10. extension = e;
11. }
12.
13. protected Phone(int n, int e, String r) {
14. this(n, e);
15. ringTone = r;
16. }
17.
18. void placeCall(int numberToDial) {
19. System.out.println(“Calling “ + numberToDial);
20. }
21.
22. protected String getRingTone() {
23. return ringTone;
24. }
25. }
The Phone class has the following properties:
The class is public , so it is accessible from anywhere. More precisely, the Phone class
can be used in any other class.
Its number field and the constructor on line 8 are also public , so they are accessible
from any other class.
The ringTone field is private and only accessible from within the class, as done on
lines 15 and 23.
The extension field has the default access and is only accessible from other classes in
the com.sybex.demos package.
The constructor on line 13 and the getRingTone method are protected , so they are
accessible from any child classes of Phone and also any other classes in the com.sybex
.demos package.
Search WWH ::




Custom Search