Implementing an interface is like signing a contract with the compiler that states, "I will declare all the methods specified by the interface or I will declare my class abstract."įailing to implement any method of an interface in a concrete class that implements the interface results in a compilation error indicating that the class must be declared abstract. A class that does not implement all the methods of the interface is an abstract class and must be declared abstract. To specify that a class implements an interface add the implements keyword and the name of the interface to the end of your class declaration's first line. To use an interface, a concrete class must specify that it implements the interface and must declare each method in the interface with the signature specified in the interface declaration. Similarly, constants should be declared without keywords public, static and final, because they, too, are redundant. Īccording to Chapter 9 of the Java Language Specification, it's proper style to declare an interface's methods without keywords public and abstract, because they're redundant in interface method declarations. All methods declared in an interface are implicitly public abstract methods, and all fields are implicitly public, static and final. Unlike classes, all interface members must be public, and interfaces may not specify any implementation details, such as concrete method declarations and instance variables. An interface declaration begins with the keyword interface and contains only constants and abstract methods. The next example introduces an interface named Payable to describe the functionality of any object that must be capable of being paid and thus must offer a method to determine the proper payment amount due. A Java interface describes a set of methods that can be called on an object to tell it, for example, to perform some task or return some piece of information. Software objects also communicate via interfaces. Software Objects Communicate Via Interfaces The interface specifies what operations a radio must permit users to perform but does not specify how the operations are performed. The controls allow users to perform only a limited set of operations (e.g., change the station, adjust the volume, choose between AM and FM), and different radios may implement the controls in different ways (e.g., using push buttons, dials, voice commands). For example, the controls on a radio serve as an interface between radio users and a radio's internal components. Interfaces define and standardize the ways in which things such as people and systems can interact with one another. Can we calculate such different things as the payments due for employees and invoices in a single application polymorphically? Does Java offer a capability requiring that unrelated classes implement a set of common methods (e.g., a method that calculates a payment amount)? Java interfaces offer exactly this capability. For an invoice, the payment refers to the total cost of the goods listed on the invoice. For an employee, the payment refers to the employee's earnings. Though applied to unrelated things (i.e., employees and invoices), both operations have to do with obtaining some kind of payment amount. Suppose that the company involved wishes to perform several accounting operations in a single accounts payable application-in addition to calculating the earnings that must be paid to each employee, the company must also calculate the payment due on each of several invoices (i.e., bills for goods purchased). 10.11–10.15) reexamines the payroll system of Section 10.5. Learn More Buy 10.7 Case Study: Creating and Using Interfaces
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |