What Is The Interface Concept

Explain what is interface concept.

Answer:

Interfaces only describe the external point of contact of a class (protocols), they do not contain any implementation. Interfaces are usually defined by a user. The user describes in the interface which services (technical and semantic) it needs in order to carry out a task. 

The user never actually knows the providers of these services, but communicates with them through the interface. In this way the user is protected from actual implementations and can work in the same way with different classes/objects, as long as they provide the services required. This is known as polymorphism with interfaces. 

In ABAP interfaces are implemented in addition to, and independently of classes. An interface only has a declaration part, and do not have visibility sections. Components (Attributes, methods, constants, types) can be defined the same way as in classes.

- Interfaces are listed in the definition part of the class, and must always be in the PUBLIC SECTION.

- Operations defined in the interface are implemented as methods of the class. All methods of the interface must be present in the implementation part of the class.

- Attributes, events, constants and types defined in the interface are automatically available to the class carrying out the implementation.

- Interface components are addressed in the class by <interface name>~<component name> 
 

Example of how to implement an interface:

INTERFACE lif_document

DATA: author type ref to lcl_author.

METHODS: print,

display.

ENDINTERFACE.
 

CLASS lcl_text_document DEFINITION.

PUBLIC SECTION.

INTERFACES lif_document.

METHODS display.

ENDCLASS.
 

CLASS lcl_text_document IMPLEMENTTION.

METHOD lif_document~print.

ENDMETHOD.

METHOD lif_document~display

ENDMETHOD.

METHOD display.

ENDMETHOD.

ENDCLASS.
 

REPORT zzz.

DATA: text_doc TYPE REF TO lcl_document.

Start-of-selection.

CREATE OBJECT text_doc.

CALL METHOD text_doc->lif_document~print.

CALL METHOD text_doc->lif_document~display.

CALL METHOD text_doc->display.

ABAP Tips

See Also
Difference Between Changing And Using

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Main Index
SAP Basis, ABAP Programming and Other IMG Stuff

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.