You are on page 1of 12

IBM Global Business Services

ABAP Objects Advanced - Interfaces

IBM Corporation 2013


IBM Global Business Services

ABAP Objects Advanced : Interface Topics

Concept
Defining Interfaces
Implementing Interface in Classes
Compound Interfaces
Alias Names
Interface References
Polymorphism through interfaces
Interface and Inheritance

2 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Interfaces : Concepts

Interface is similar to abstract


Interface I1
class but it has only definitions
DATA: part.
CLASS-DATA: Class C1
Interface, which is an independent
METHODS: Public: structure, is used to implement in a
CLASS-METHODS: Interfaces I1 class to extend the scope of a class.
EVENTS:
Interface allows users to address
CLASS-EVENTS: different classes via a universal
Class C2
point of contact.
Public: All components of an interface are
Interfaces I1 public.
In ABAP Objects interfaces serve to
Class C3 define uniform protocol for
services. Various Classes can offer
Public: these services in different ways, but
Interfaces I1 keep the same semantics.

3 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Interfaces : Defining Interfaces


INTERFACE I1
DATA: Interfaces are defined as independent
constructs, in an INTERFACE.
CLASS-DATA:
ENDINTERFACE block similar to the
METHODS: declaration block of classes.
CLASS-METHODS:
An interface can declare instance as
EVENTS: well as static components like
CLASS-EVENTS: classes.
ENDINTERFACE Interface components are always
PUBLIC, so visibility is not explicitly
INTERFACE I2. defined.
DATA : name(20). Interface define the methods but do
METHODS: I_method1, not Implement them. Similarly how
I_method2. Sub Classes implement the methods
ENDINTERFACE. of an abstract class, all classes that
wish to use an interface must
implement all of its methods.

4 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Interfaces : Implementing Interface in Classes


INTERFACE my_interface . Each class can implement one or
METHODS my_interface_method. more interfaces. Also, multiple
ENDINTERFACE. classes can implement the same
interface.
CLASS interface_class DEFINITION. The INTERFACES statement should
PUBLIC SECTION. be In the PUBLIC section, expanding
INTERFACES my_interface. the public visibility section of the
ENDCLASS. class definition.
The class must implement all the
methods of each incorporated
CLASS interface_class
IMPLEMENTATION. interface.
METHOD Within the class each component of
my_interface~my_interface_method. the interface is identified by the name
DATA: num TYPE I VALUE 10. intf~comp.so identically name
Write:/ num. componets of different interface do no
ENDMETHOD. conflict when implemented in the
same class. ~ is called interface
ENDCLASS. component selector.
5 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013
IBM Global Business Services

Interfaces : Defining and Implementing Compound Interfaces


INTERFACE I1 .
METHODS meth. A new interface can be created from
ENDINTERFACE. several existing interface. Such an
INTERFACE I2 .
interface is called a compound
METHODS meth.
interface. An interface which is
INTERFACES I1.
ENDINTERFACE.
contained in another interface is
CLASS interface_class DEFINITION. called component interface.
PUBLIC SECTION. In a compound interface there is
INTERFACES: I2. no component hierarchy. All
ENDCLASS.
component interfaces are on the
CLASS interface_class IMPLEMENTATION.
METHOD I1~meth.
same level. So it is not possible to
. chain names such as I2~I1.
ENDMETHOD. A compound interface contains each
METHOD I2~meth. component interface only once.
.
ENDMETHOD. When a class implements interfaces,
ENDCLASS. each component interface is
implemented only once.

6 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Interfaces : Alias names


INTERFACE I1 .
METHODS m1.
Full name of a component in the
ENDINTERFACE.
interface when added in class or
INTERFACE I2. another interface is intf~comp, Alias
METHODS : m2.
INTERFACES I1. names can be used to replaced this
ALIASES meth1 FOR I1~m1. names when defining compound
ENDINTERFACE. interface or declaring interfaces in a
CLASS c1 DEFINITION. class.
PUBLIC SECTION.
INTERFACES : I2.
The class implementation must still
ALIASES meth2 FOR I2~m2. refer to the full name.
ENDCLASS.

CLASS C1 IMPLEMENTATION.
METHOD I1~m1. START-OF-SELECTION.
... DATA : oref TYPE REF TO c1
ENDMETHOD. CREATE OBJECT oref.
CALL METHOD : oref->I2~meth1.
METHOD : I2~m2.
CALL METHOD : oref->meth2 .
...
ENDMETHOD.
ENDCLASS.

7 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Interfaces : Interface References


INTERFACE I1.
METHODS : m1. As class reference variables are
ENDINTERFACE. declared with the static type of a
CLASS c1 DEFINITION.
class (oref TYPE REF TO C1),
PUBLIC SECTION.
interface reference variables are
INTERFACES : I1.
ENDCLASS. declared with the static type of a
interface (iref TYPE REF TO I1).
CLASS C1 IMPLEMENTATION. Interface reference variables like
METHOD I1~m1.
class reference variables can contain
...
object references, which determine
ENDMETHOD.
ENDCLASS.
their dynamic type (Iref = oref. or
START-OF-SELECTION. CREATE OBJECT iref TYPE C1.)
DATA : oref TYPE REF TO C1, When Interface reference variables
iref TYPE REF TO I1. are dynamic and contain object
CREATE OBJECT oref.
references, at this time it can only
CALL METHOD : oref->I1~m1.
Iref = oref.
access the interface components
CALL METHOD : iref->m1. implemented in the class of that
object. (Narrowing Cast)
8 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013
IBM Global Business Services

Interfaces : Polymorphism
INTERFACE I1.
METHODS : M1 .
ENDINTERFACE. Interfaces allow to use different
CLASS C1 DEFINITION.
PUBLIC SECTION. classes in a uniform way using
INTERFACES : I1.
ENDCLASS.
interface references. Any no of class
CLASS C1 IMPLEMENTATION.
can implement the interface
METHOD I1~M1. differently.
.
ENDMETHOD.
ENDCLASS.
The identical interface reference
CLASS C2 DEFINITION. variable, statically typed to this
PUBLIC SECTION.
INTERFACES : I1. interface, operates on multiple
ENDCLASS.
CLASS C2 IMPLEMENTATION. objects that implement the interface.
METHOD I1~M1.
.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : OREF1 TYPE REF TO C1 ,
OREF2 TYPE REF TO C2 ,
IREF TYPE REF TO I1 .
CREATE OBJECT : OREF1 ,
OREF2 .
IREF = OREF1.
CALL METHOD IREF->M1.
IREF = OREF2.
CALL METHOD IREF->M1.

9 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Interfaces and Inheritance

Interfaces can be implemented in the classes of an inheritance tree, each


interface only once ( each interface component should have a unique name
through out the inheritance tree).
If it is not suitable to link classes using Inheritance, they can be linked with
Interfaces
Interfaces separate Protocol (Interface-often defined by the user) and Service
(Implementing Class)
Interface provides option of simulating Multiple Inheritance.

10 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Demonstration

Exercise 7:
In this exercise you will demonstrate Polymorphism through Interfaces.

11 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013


IBM Global Business Services

Practice

Working with interfaces in ABAP objects

12 ABAP Objects - Advanced Jan-2007 IBM Corporation 2013

You might also like