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

ABAP Objects - Advanced

Jan-2007

IBM Corporation 2013

IBM Global Business Services

Interfaces : Concepts
Interface I1
DATA:
CLASS-DATA:
METHODS:
CLASS-METHODS:
EVENTS:
CLASS-EVENTS:

Class C1
Public:
Interfaces I1

Class C2
Public:
Interfaces I1

Class C3
Public:
Interfaces I1
3

ABAP Objects - Advanced

Interface is similar to abstract class


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

Jan-2007

IBM Corporation 2013

IBM Global Business Services

Interfaces : Defining Interfaces


INTERFACE I1
DATA:
CLASS-DATA:
METHODS:
CLASS-METHODS:
EVENTS:
CLASS-EVENTS:
ENDINTERFACE
INTERFACE I2.
DATA : name(20).
METHODS: I_method1,
I_method2.
ENDINTERFACE.

ABAP Objects - Advanced

Interfaces are defined as independent


constructs, in an INTERFACE.
ENDINTERFACE block similar to the
declaration block of classes.
An interface can declare instance as
well as static components like
classes.
Interface components are always
PUBLIC, so visibility is not explicitly
defined.
Interface define the methods but do
not Implement them. Similarly how
Sub Classes implement the methods
of an abstract class, all classes that
wish to use an interface must
implement all of its methods.
Jan-2007

IBM Corporation 2013

IBM Global Business Services

Interfaces : Implementing Interface in Classes


INTERFACE my_interface .
METHODS my_interface_method.
ENDINTERFACE.
CLASS interface_class DEFINITION.
PUBLIC SECTION.
INTERFACES my_interface.
ENDCLASS.

CLASS interface_class
IMPLEMENTATION.
METHOD
my_interface~my_interface_method.
DATA: num TYPE I VALUE 10.
Write:/ num.
ENDMETHOD.
ENDCLASS.
5

ABAP Objects - Advanced

Each class can implement one or


more interfaces. Also, multiple
classes can implement the same
interface.
The INTERFACES statement should
be In the PUBLIC section, expanding
the public visibility section of the
class definition.
The class must implement all the
methods of each incorporated
interface.
Within the class each component of
the interface is identified by the name
intf~comp.so identically name
componets of different interface do no
conflict when implemented in the
same class. ~ is called interface
component selector.
Jan-2007

IBM Corporation 2013

IBM Global Business Services

Interfaces : Defining and Implementing Compound Interfaces


INTERFACE I1 .
METHODS meth.
ENDINTERFACE.
INTERFACE I2 .
METHODS meth.
INTERFACES I1.
ENDINTERFACE.
CLASS interface_class DEFINITION.
PUBLIC SECTION.
INTERFACES: I2.
ENDCLASS.
CLASS interface_class IMPLEMENTATION.
METHOD I1~meth.
.
ENDMETHOD.
METHOD I2~meth.
.
ENDMETHOD.
ENDCLASS.

ABAP Objects - Advanced

A new interface can be created from


several existing interface. Such an
interface is called a compound
interface. An interface which is
contained in another interface is
called component interface.
In a compound interface there is
no component hierarchy. All
component interfaces are on the
same level. So it is not possible to
chain names such as I2~I1.
A compound interface contains each
component interface only once.
When a class implements interfaces,
each component interface is
implemented only once.
Jan-2007

IBM Corporation 2013

IBM Global Business Services

Interfaces : Alias names


INTERFACE I1 .
METHODS m1.
ENDINTERFACE.

Full name of a component in the


interface when added in class or
another interface is intf~comp, Alias
names can be used to replaced this
names when defining compound
interface or declaring interfaces in a
class.
The class implementation must still
refer to the full name.

INTERFACE I2.
METHODS : m2.
INTERFACES I1.
ALIASES meth1 FOR I1~m1.
ENDINTERFACE.
CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES : I2.
ALIASES meth2 FOR I2~m2.
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD I1~m1.
...
ENDMETHOD.
METHOD : I2~m2.
...
ENDMETHOD.
ENDCLASS.
7

START-OF-SELECTION.
DATA : oref TYPE REF TO c1
CREATE OBJECT oref.
CALL METHOD : oref->I2~meth1.
CALL METHOD : oref->meth2 .

ABAP Objects - Advanced

Jan-2007

IBM Corporation 2013

IBM Global Business Services

Interfaces : Interface References


INTERFACE I1.
METHODS : m1.
ENDINTERFACE.
CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES : I1.
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD I1~m1.
...
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA : oref TYPE REF TO C1,
iref TYPE REF TO I1.
CREATE OBJECT oref.
CALL METHOD : oref->I1~m1.
Iref = oref.
CALL METHOD : iref->m1.

ABAP Objects - Advanced

As class reference variables are


declared with the static type of a class
(oref TYPE REF TO C1), interface
reference variables are declared with
the static type of a interface (iref
TYPE REF TO I1).
Interface reference variables like
class reference variables can contain
object references, which determine
their dynamic type (Iref = oref. or
CREATE OBJECT iref TYPE C1.)
When Interface reference variables
are dynamic and contain object
references, at this time it can only
access the interface components
implemented in the class of that
object. (Narrowing Cast)
Jan-2007

IBM Corporation 2013

IBM Global Business Services

Interfaces : Polymorphism
INTERFACE I1.
METHODS : M1 .
ENDINTERFACE.
CLASS C1 DEFINITION.
PUBLIC SECTION.
INTERFACES : I1.
ENDCLASS.
CLASS C1 IMPLEMENTATION.
METHOD I1~M1.
.
ENDMETHOD.
ENDCLASS.
CLASS C2 DEFINITION.
PUBLIC SECTION.
INTERFACES : I1.
ENDCLASS.
CLASS C2 IMPLEMENTATION.
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.

ABAP Objects - Advanced

Interfaces allow to use different


classes in a uniform way using
interface references. Any no of class
can implement the interface
differently.
The identical interface reference
variable, statically typed to this
interface, operates on multiple
objects that implement the interface.

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