1 ObjectOriented Programming Interface 2005 Pearson Education Inc
1 Object-Oriented Programming: Interface 2005 Pearson Education, Inc. All rights reserved.
2 10. 7. 1 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 Interface’s method name is better to describe the method’s 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 Payable interface Contains method get. Payment. Amount Is implemented by the Invoice and Employee classes Fig. 10 | Payable interface hierarchy UML class diagram. 2005 Pearson Education, Inc. All rights reserved.
Outline 5 Declare interface Payable. java Declare get. Payment. Amount 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.
Outline 6 Class Invoice implements interface Payable Invoice. java (1 of 3) 2005 Pearson Education, Inc. All rights reserved.
Outline 7 Invoice. java (2 of 3) 2005 Pearson Education, Inc. All rights reserved.
Outline 8 Invoice. java (3 of 3) Declare get. Payment. Amount to fulfill contract with interface Payable 2005 Pearson Education, Inc. All rights reserved.
9 10. 7. 3 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 Class. Name extends Superclass. Name implements First. Interface, Second. Interface, … 2005 Pearson Education, Inc. All rights reserved.
Outline Class Employee implements interface Payable 10 Employee. java (1 of 3) 2005 Pearson Education, Inc. All rights reserved.
Outline 11 Employee. java (2 of 3) 2005 Pearson Education, Inc. All rights reserved.
Outline 12 Employee. java (3 of 3) get. Payment. Amount method is not implemented here 2005 Pearson Education, Inc. All rights reserved.
10. 7. 5 Modifying Class Salaried. Employee for Use in the Payable Hierarchy 13 • 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.
Outline 14 Class Salaried. Employee extends class Employee (which implements interface Payable) Salaried. Employee. java (1 of 2) 2005 Pearson Education, Inc. All rights reserved.
Outline 15 Salaried. Employee. java Declare get. Payment. Amount method instead of earnings method (2 of 2) 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.
Outline 17 Declare array of Payable variables Payable. Interface Test. java Assigning references to (1 of 2) Invoice objects to Payable variables Assigning references to Salaried. Employee objects to Payable variables 2005 Pearson Education, Inc. All rights reserved.
Outline 18 Payable. Interface Test. java Call to. String and get. Payment. Amount methods polymorphically (2 of 2) 2005 Pearson Education, Inc. All rights reserved.
10. 7. 7 Declaring Constants with Interfaces 19 • 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 package. Name. Class. Name. static. Member. Name; • import static package. Name. Class. Name. *; 2005 Pearson Education, Inc. All rights reserved.
20 Example on static import static java. lang. Math. *; public class Static. Import. Test { public static void main( String args[] ) { System. out. printf( "sqrt( 900. 0 ) = %. 1 fn", sqrt( 900. 0 ) ); System. out. printf( "ceil( -9. 8 ) = %. 1 fn", ceil( -9. 8 ) ); } // end main } // end class Static. Import. Tes 2005 Pearson Education, Inc. All rights reserved.
21 2005 Pearson Education, Inc. All rights reserved.
- Slides: 21