Interface

From Abap2Java

Jump to: navigation, search

Contents

Image:Dotright.JPG ABAP

see INTERFACE

Image:Dotleft.JPG Java

Interfaces allow the detachment of behaviour and implementation by adding a layer between a the caller and the callee.

+------------------------------------------+
| Enumeration enum = v.elements();         |
| while (enumu.hasMoreElements())          |
|    ...                                   |
+------------------------------------------+
         |                       |
+------------------------------------------+
+ interface Enumeration {                  +
+    boolean hasMoreElements();            +
+    ...                                   +
--------------------------------------------
         |                       |
--------------------------------------------
+ class MyEnumeration                      +
+    implements Enumeration                +
--------------------------------------------              


Consuming interfaces

As a consumer you do not program against an object and its methods but against an interface. A good example is JDBC. A set of interfaces that describe the behaviour to connect to a database and query it. The database vendor implements those interfaces and the underlying databases is replaceable by telling what specific implementation shall be used.


Providing interfaces

Java defines several useful interfaces that can be implemented by your class to add behaviour. Standard classes like a Set or a Sorted Map will be able to sort your custom class if you implement the interface Comparable.

interface java.lang.Comparable {
   int compareTo(Object o);
}
class Client implements Comparable {
   ...
   int compareTo(Obect o) {
      Client c = (Client) o;
      // do individual comparasion
      return r; // r = {-1,0,1}
   }
}

The example added the capability of comparing objects of the class Client. This allows a set or a sorted map to sort members.

Furthermore this concept is used for components (EJB, Servlet) where a runtime environment will work with interface for which you provide the implementation. (see component) For example the webcontainer of a J2EE engine will call the method doPost(...) as soon as a specific http Request request reaches the server.


External links
Interface Comparable

Personal tools