【正文】
class – However, an interface is not a class – It is a type that can be satisfied by any class that implements the interface ? The syntax for defining an interface is similar to that of defining a class – Except the word interface is used in place of class ? An interface specifies a set of methods that any class that implements the interface must have – It contains method headings and constant definitions only – It contains no instance variables nor any plete method definitions 132 Copyright 169。s responsibility to follow the semantics of the Comparable interface when implementing it 1315 Copyright 169。 2021 Pearson AddisonWesley. All rights reserved. The Cloneable Interface ? The method () does a bitbybit copy of the object39。 2021 Pearson AddisonWesley. All rights reserved. Pitfall: Inconsistent Interfaces ? In Java, a class can have only one base class – This prevents any inconsistencies arising from different definitions having the same method heading ? In addition, a class may implement any number of interfaces – Since interfaces do not have method bodies, the above problem cannot arise – However, there are other types of inconsistencies that can arise 1327 Copyright 169。 2021 Pearson AddisonWesley. All rights reserved. Pitfall: Interface Semantics Are Not Enforced ? When a class implements an interface, the piler and runtime system check the syntax of the interface and its implementation – However, neither checks that the body of an interface is consistent with its intended meaning ? Required semantics for an interface are normally added to the documentation for an interface – It then bees the responsibility of each programmer implementing the interface to follow the semantics ? If the method body does not satisfy the specified semantics, then software written for classes that implement the interface may not work correctly 1313 Copyright 169。 2021 Pearson AddisonWesley. All rights reserved. Interfaces ? An interface serves a function similar to a base class, though it is not a base class – Some languages allow one class to be derived from two or more different base classes – This multiple inheritance is not allowed in Java – Instead, Java39。 2021 Pearson AddisonWesley. All rights reserved. The Comparable Interface Semantics ? The method pareTo must return – A negative