2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.

Презентация:



Advertisements
Похожие презентации
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Polymorphism.
Advertisements

2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
1 © Luxoft Training 2012 Inner and anonymous classes.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
1/27 Chapter 9: Template Functions And Template Classes.
1 When And How To Make Interfaces Work For You Advanced OO Techniques Alex Herbstritt Principal Software Engineer, Progress Software Session 102.
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
© Luxoft Training 2013 Using Reflection API in Java.
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
Loader Design Options Linkage Editors Dynamic Linking Bootstrap Loaders.
© 2005 Cisco Systems, Inc. All rights reserved. BGP v Scaling Service Provider Networks Designing Networks with Route Reflectors.
© 2006 Cisco Systems, Inc. All rights reserved. HIPS v Configuring Groups and Policies Managing Hosts and Deploying Software Updates.
© 2006 Cisco Systems, Inc. All rights reserved. MPLS v MPLS VPN Implementation Configuring VRF Tables.
© 2006 Cisco Systems, Inc. All rights reserved. HIPS v Creating Application Classes Working with Variables and Application Classes.
Chap 8-1 Statistics for Business and Economics, 6e © 2007 Pearson Education, Inc. Chapter 8 Estimation: Single Population Statistics for Business and Economics.
© 2005 Cisco Systems, Inc. All rights reserved.INTRO v Constructing Network Addresses Calculating Subnet Masks.
© Luxoft Training 2013 Java Collections API. © Luxoft Training 2013 Collections hierarchy.
© 2006 Cisco Systems, Inc. All rights reserved. BSCI v Implementing IPv6 Implementing Dynamic IPv6 Addresses.
Транксрипт:

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