2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface
2005 Pearson Education, Inc. All rights reserved Developing a Payable Hierarchy An interface is a group of related methods with empty bodies, and might also contain constant definitions. Interfaces form a contract between the class and the outside world. an interface can extend any number of interfaces. UML representation of interfaces – Interfaces are distinguished from classes by placing the word interface (« and ») above the interface name – The relationship between a class and an interface is known as realization A class realizes the method of an interface
2005 Pearson Education, Inc. All rights reserved. 3 Good Programming Practice 10.2 Interfaces method name is better to describe the methods purpose in a general manner, because the method may be implemented by a broad range of unrelated classes.
2005 Pearson Education, Inc. All rights reserved. 4 Fig | Payable interface hierarchy UML class diagram. Payable interface Contains method getPaymentAmount Is implemented by the Invoice and Employee classes
2005 Pearson Education, Inc. All rights reserved. 5 Outline Payable.java Declare interface Payable Declare getPaymentAmount method which is implicitly public and abstract An interface can contain constant declarations in addition to method declarations. All constant values defined in an interface are implicitly public, static, and final.
2005 Pearson Education, Inc. All rights reserved. 6 Outline Invoice.java (1 of 3) Class Invoice implements interface Payable
2005 Pearson Education, Inc. All rights reserved. 7 Outline Invoice.java (2 of 3)
2005 Pearson Education, Inc. All rights reserved. 8 Outline Invoice.java (3 of 3) Declare getPaymentAmount to fulfill contract with interface Payable
2005 Pearson Education, Inc. All rights reserved Creating Class Invoice A class can implement as many interfaces as it needs – Use a comma-separated list of interface names after keyword implements Example: public class ClassName extends SuperclassName implements FirstInterface, SecondInterface, …
2005 Pearson Education, Inc. All rights reserved. 10 Outline Employee.java (1 of 3) Class Employee implements interface Payable
2005 Pearson Education, Inc. All rights reserved. 11 Outline Employee.java (2 of 3)
2005 Pearson Education, Inc. All rights reserved. 12 Outline Employee.java (3 of 3) getPaymentAmount method is not implemented here
2005 Pearson Education, Inc. All rights reserved Modifying Class SalariedEmployee for Use in the Payable Hierarchy Objects of any subclasses of the class that implements the interface can also be thought of as objects of the interface – A reference to a subclass object can be assigned to an interface variable if the superclass implements that interface
2005 Pearson Education, Inc. All rights reserved. 14 Outline SalariedEmployee.java (1 of 2) Class SalariedEmployee extends class Employee (which implements interface Payable )
2005 Pearson Education, Inc. All rights reserved. 15 Outline SalariedEmployee.java (2 of 2) Declare getPaymentAmount method instead of earnings method
2005 Pearson Education, Inc. All rights reserved. 16 Software Engineering Observation 10.8 When a method parameter receives a variable of interface type, any object of a class that implements the interface may be passed as an argument. When a method parameter receives a variable of a superclass type, any object of a subclass may be passed as an argument. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.
2005 Pearson Education, Inc. All rights reserved. 17 Outline PayableInterface Test.java (1 of 2) Declare array of Payable variables Assigning references to Invoice objects to Payable variables Assigning references to SalariedEmployee objects to Payable variables
2005 Pearson Education, Inc. All rights reserved. 18 Outline PayableInterface Test.java (2 of 2) Call toString and getPaymentAmount methods polymorphically
2005 Pearson Education, Inc. All rights reserved Declaring Constants with Interfaces Interfaces can be used to declare related constants used in many class declarations – These constants are implicitly public, static and final – Using a static import declaration allows clients to use these constants with just their names – A static import: class name and a dot (.) are not required to use an imported static member. – Format: import static packageName.ClassName.staticMemberName; import static packageName.ClassName.*;
2005 Pearson Education, Inc. All rights reserved. Example on static import 20 import static java.lang.Math.*; public class StaticImportTest { public static void main( String args[] ) { System.out.printf( "sqrt( ) = %.1f\n", sqrt( ) ); System.out.printf( "ceil( -9.8 ) = %.1f\n", ceil( -9.8 ) ); } // end main } // end class StaticImportTes
2005 Pearson Education, Inc. All rights reserved. 21