Download presentation
Presentation is loading. Please wait.
1
Object-Oriented Programming: Interface
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 If an interface is inherited from specific interface, then all classes that implement the sub-interface must re-write the methods in both interfaces (super and sub interfaces)
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.
4
Fig. 10.10 | Payable interface hierarchy UML class diagram.
Payable interface Contains method getPaymentAmount Is implemented by the Invoice and Employee classes Fig | Payable interface hierarchy UML class diagram.
5
Outline Declare interface Payable Payable.java 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. All methods declared in an interface are implicitly public, so the public modifier can be omitted.
6
Outline Class Invoice implements interface Payable (1 of 3)
Invoice.java (1 of 3)
7
Outline Invoice.java (2 of 3)
8
Outline Invoice.java (3 of 3) Declare getPaymentAmount to fulfill contract with interface Payable
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 ClassName extends SuperclassName implements FirstInterface, SecondInterface, …
10
Outline Class Employee implements interface Payable (1 of 3)
Employee.java (1 of 3)
11
Outline Employee.java (2 of 3)
12
Outline (3 of 3) getPaymentAmount method is not implemented here
Employee.java (3 of 3) getPaymentAmount method is not implemented here
13
10.7.5 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 The usefulness of interfaces goes far beyond simply publishing protocols for other programmers. Any function can have parameters that are of interface type. Any object of a class that implements the interface may be passed as an argument.
14
Outline Class SalariedEmployee extends class Employee (which implements interface Payable) SalariedEmployee .java (1 of 2)
15
Outline SalariedEmployee .java (2 of 2) Declare getPaymentAmount method instead of earnings method
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.
17
Outline Declare array of Payable variables (1 of 2)
PayableInterface Test.java (1 of 2) Assigning references to Invoice objects to Payable variables Assigning references to SalariedEmployee objects to Payable variables 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.
18
Outline PayableInterface Test.java (2 of 2) Call toString and getPaymentAmount methods polymorphically
19
10.7.7 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.*; Static import declaration enables programmers to refer to imported static members as if they were declared in the class that uses them—the
20
Example on static import
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.