《[所有分类]墨尔本大学java课程课件chap》由会员分享,可在线阅读,更多相关《[所有分类]墨尔本大学java课程课件chap(35页珍藏版)》请在金锄头文库上搜索。
1、Chapter 13,Interfaces,Copyright 2010 Pearson Addison-Wesley. All rights reserved.,Interfaces,An interface is something like an extreme case of an abstract 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
2、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 c
3、omplete method definitions,13-2,Copyright 2010 Pearson Addison-Wesley. 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
4、is not allowed in Java Instead, Javas way of approximating multiple inheritance is through interfaces,13-3,Copyright 2010 Pearson Addison-Wesley. All rights reserved.,Interfaces,An interface and all of its method headings should be declared public They cannot be given private, protected, or package
5、access When a class implements an interface, it must make all the methods in the interface public Because an interface is a type, a method may be written with a parameter of an interface type That parameter will accept as an argument any class that implements the interface,13-4,Copyright 2010 Pearso
6、n Addison-Wesley. All rights reserved.,The Ordered Interface,13-5,Copyright 2010 Pearson Addison-Wesley. All rights reserved.,Interfaces,To implement an interface, a concrete class must do two things: It must include the phrase implements Interface_Name at the start of the class definition If more t
7、han one interface is implemented, each is listed, separated by commas The class must implement all the method headings listed in the definition(s) of the interface(s) Note the use of Object as the parameter type in the following examples,13-6,Copyright 2010 Pearson Addison-Wesley. All rights reserve
8、d.,Implementation of an Interface,13-7,Copyright 2010 Pearson Addison-Wesley. All rights reserved.,Implementation of an Interface,13-8,Copyright 2010 Pearson Addison-Wesley. All rights reserved.,Abstract Classes Implementing Interfaces,Abstract classes may implement one or more interfaces Any method
9、 headings given in the interface that are not given definitions are made into abstract methods A concrete class must give definitions for all the method headings given in the abstract class and the interface,13-9,Copyright 2010 Pearson Addison-Wesley. All rights reserved.,An Abstract Class Implement
10、ing an Interface,13-10,Copyright 2010 Pearson Addison-Wesley. All rights reserved.,Derived Interfaces,Like classes, an interface may be derived from a base interface This is called extending the interface The derived interface must include the phrase extends BaseInterfaceName A concrete class that i
11、mplements a derived interface must have definitions for any methods in the derived interface as well as any methods in the base interface,13-11,Copyright 2010 Pearson Addison-Wesley. All rights reserved.,Extending an Interface,13-12,Copyright 2010 Pearson Addison-Wesley. All rights reserved.,Pitfall
12、: Interface Semantics Are Not Enforced,When a class implements an interface, the compiler and run-time 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
13、 normally added to the documentation for an interface It then becomes 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 corr
14、ectly,13-13,Copyright 2010 Pearson Addison-Wesley. All rights reserved.,The Comparable Interface,Chapter 6 discussed the Selection Sort algorithm, and examined a method for sorting a partially filled array of type double into increasing order This code could be modified to sort into decreasing order
15、, or to sort integers or strings instead Each of these methods would be essentially the same, but making each modification would be a nuisance The only difference would be the types of values being sorted, and the definition of the ordering Using the Comparable interface could provide a single sorti
16、ng method that covers all these cases,13-14,Copyright 2010 Pearson Addison-Wesley. All rights reserved.,The Comparable Interface,The Comparable interface is in the java.lang package, and so is automatically available to any program It has only the following method heading that must be implemented: public int compareTo(Object other); It is the programmers responsibility to follow the semantics of the Comparable interface when implementing it,13-15,Copyright 2010 Pe