Java Reference
In-Depth Information
Answers to Self-Test Exercises
1. Yes to both. An interface is a type and can be used like any other type.
2. Yes. Any of the interface methods that it does not fully define must be made
abstract methods.
3. A derived class can have only one base class, but it can implement any number of
interfaces.
4. No, but the way to accomplish the same thing is to have one interface extend the
other.
These exercises are for the material on the
Comparable
interface.
5. Yes, the dialogue would be the same. The change from the parameter type
Comparable[]
to
Object[]
in the method
interchange
is in fact a good idea.
6. No. This will compile without any error messages. However, the less-than order-
ing does not satisfy the semantics of the
Comparable
interface. For example, the
trichotomy law does not hold.
7. Yes. The three required conditions are true for objects of the class
Circle
:
(Irreflexive) By definition, no circle is inside itself.
(Trichotomy) For any two circles
c1
and
c2
with centers at the origin, one and only
one of the following holds true:
c1
is inside of
c2,
c2
is inside of
c1
, or
c1
equals
c2
.
(Transitivity) If
c1
is inside of
c2
and
c2
is inside of
c3
, then
c1
is inside of
c3
.
8. The class will produce a compiler error message saying that there is an inconsis-
tency in the definitions of
ANSWER
.
9. The class will compile and run with no error messages. Since the named constant
ANSWER
is never used, there is no inconsistency.
10. The class will produce a compiler error message saying that you have not imple-
mented the heading for
getStuff
in
InterfaceA
.
11. They will all compile and the program will run. The two definitions of
getStuff
have different numbers of parameters and so this is overloading. There is no
inconsistency.
12.
public class
StockItem
implements
Cloneable
{
private int
number;
private
String name;
public void
setNumber(
int
newNumber)
{
number = newNumber;
}
...
public
Object clone()
{
try
{