12
© IBM Corporation 2013 IBM Global Business Services ABAP Objects Advanced - Interfaces

04 ABAP Objects -Interfaces

Embed Size (px)

DESCRIPTION

04 ABAP Objects -Interfaces

Citation preview

Page 1: 04 ABAP Objects -Interfaces

© IBM Corporation 2013

IBM Global Business Services

ABAP Objects Advanced - Interfaces

Page 2: 04 ABAP Objects -Interfaces

IBM Global Business Services

© IBM Corporation 20132 Jan-2007ABAP Objects - Advanced

ABAP Objects Advanced : Interface Topics

Concept

Defining Interfaces

Implementing Interface in Classes

Compound Interfaces

Alias Names

Interface References

Polymorphism through interfaces

Interface and Inheritance

Page 3: 04 ABAP Objects -Interfaces

IBM Global Business Services

© IBM Corporation 20133 Jan-2007ABAP Objects - Advanced

Interfaces : Concepts

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.

Public:

Interfaces I1

Class C1

Public:

Interfaces I1

Class C2

DATA:

CLASS-DATA:

METHODS:

CLASS-METHODS:

EVENTS:

CLASS-EVENTS:

Interface I1

Public:

Interfaces I1

Class C3

Page 4: 04 ABAP Objects -Interfaces

IBM Global Business Services

© IBM Corporation 20134 Jan-2007ABAP Objects - Advanced

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.

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.

Page 5: 04 ABAP Objects -Interfaces

IBM Global Business Services

© IBM Corporation 20135 Jan-2007ABAP Objects - Advanced

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.

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.

Page 6: 04 ABAP Objects -Interfaces

IBM Global Business Services

© IBM Corporation 20136 Jan-2007ABAP Objects - Advanced

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.

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.

Page 7: 04 ABAP Objects -Interfaces

IBM Global Business Services

© IBM Corporation 20137 Jan-2007ABAP Objects - Advanced

Interfaces : Alias names

INTERFACE I1 . METHODS m1.ENDINTERFACE.

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.

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.

START-OF-SELECTION.

DATA : oref TYPE REF TO c1

CREATE OBJECT oref.

CALL METHOD : oref->I2~meth1.

CALL METHOD : oref->meth2 .

Page 8: 04 ABAP Objects -Interfaces

IBM Global Business Services

© IBM Corporation 20138 Jan-2007ABAP Objects - Advanced

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.

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)

Page 9: 04 ABAP Objects -Interfaces

IBM Global Business Services

© IBM Corporation 20139 Jan-2007ABAP Objects - Advanced

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.

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.

Page 10: 04 ABAP Objects -Interfaces

IBM Global Business Services

© IBM Corporation 201310 Jan-2007ABAP Objects - Advanced

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.

Page 11: 04 ABAP Objects -Interfaces

IBM Global Business Services

© IBM Corporation 201311 Jan-2007ABAP Objects - Advanced

Demonstration

Exercise 7:

In this exercise you will demonstrate Polymorphism through Interfaces.

Page 12: 04 ABAP Objects -Interfaces

IBM Global Business Services

© IBM Corporation 201312 Jan-2007ABAP Objects - Advanced

Practice

Working with interfaces in ABAP objects