Java Reference
In-Depth Information
The following CellPhone class subclasses Phone and is in the same package, so
CellPhone has access to all the public , protected , and default members of Phone :
1. package com.sybex.demos;
2.
3. public class CellPhone extends Phone {
4. private int minutesUsed;
5. private int minutesRemaining;
6.
7. public CellPhone(int n, int minutes) {
8. super(n, 0);
9. minutesRemaining = minutes;
10. }
11.
12. public CellPhone(int n, int minutes, String ringTone) {
13. super(n, 0, ringTone);
14. minutesRemaining = minutes;
15. }
16.
17. public void placeCall(int numberToDial) {
18. super.placeCall(numberToDial);
19. minutesUsed += 10;
20. minutesRemaining -= 10;
21. }
22. }
The following comments are about the CellPhone class:
Line 8 invokes the public constructor of Phone , which is allowed because the
constructor is public .
Line 13 invokes the protected constructor of Phone , which is allowed because
CellPhone is a child of Phone .
Line 18 invokes the placeCall method of Phone , which is allowed because CellPhone
and Phone are in the same package.
Therefore, the CellPhone class compiles fi ne and is a valid extension of Phone . Now let's
look at an example that does not compile. Study the following RotaryPhone class and see if
you can determine what is wrong with the code:
1. package com.sybex.demos.oldphones;
2.
3. import com.sybex.demos.Phone;
4.
Search WWH ::




Custom Search