Interfaces Classes their instances objects and access to
Interfaces • Classes, their instances (objects), and access to objects using reference variables form the basics of ABAP Objects. • These means already allow you to model typical business applications, such as customers, order items, invoices, and so on, using objects, and to implement solutions using ABAP Objects • However, it is often necessary for similar classes to provide similar functions that are coded differently in each class but which should provide a uniform point of contact for the user.
• For example, you might have two similar classes, savings account and check account, both of which have a method for calculating end of year charges. • The interfaces and names of the methods are the same, but the actual implementation is different.
• The user of the classes and their instances must also be able to run the end of year method for all accounts, without having to worry about the actual type of each individual account. • ABAP Objects makes this possible by using interfaces. Interfaces are independent structures that you can implement in a class to extend the scope of that class. • The class-specific scope of a class is defined by its components and visibility sections. • For example, the public components of a class define its public scope, since all of its attributes and method parameters can be addressed by all users.
• The protected components of a class define its scope with regard to its subclasses. (However, inheritance is not supported in Release 4. 5 B). • Interfaces extend the scope of a class by adding their own components to its public section. • This allows users to address different classes via a universal point of contact. • Interfaces, along with inheritance, provide one of the pillars of polymorphism, since they allow a single method within an interface to behave differently in different classes.
Defining Interfaces • Global Interfaces • Local Interfaces
Defining Interfaces • Like classes, you can define interfaces either globally in the R/3 Repository or locally in an ABAP program. • For information about how to define local interfaces, refer to the Class Builder section of the ABAP Workbench Tools documentation. • The definition of a local interface <intf> is enclosed in the statements: INTERFACE <intf>. . ENDINTERFACE.
• The definition contains the declaration for all components (attributes, methods, events) of the interface. • You can define the same components in an interface as in a class. • The components of interfaces do not have to be assigned individually to a visibility section, since they automatically belong to the public section of the class in which the interface is implemented. • Interfaces do not have an implementation part, since their methods are implemented in the class that implements the interface.
Implementing Interfaces Unlike classes, interfaces do not have instances. Instead, interfaces are implemented by classes. To implement an interface in a class, use the statement INTERFACES <intf>. in the declaration part of the class. This statement may only appear in the public section of the class. • When you implement an interface in a class, the components of the interface are added to the other components in the public section. • A component <icomp> of an interface <intf> can be addressed as though it were a member of the class under the name <intf~icomp>. • • •
• The class must implement the methods of all interfaces implemented in it. • The implementation part of the class must contain a method implementation for each interface method <imeth>: METHOD <intf~imeth>. . ENDMETHOD.
• Interfaces can be implemented by different classes. • Each of these classes is extended by the same set of components. • However, the methods of the interface can be implemented differently in each class. • Interfaces allow you to use different classes in a uniform way using interface references (polymorphism). • For example, interfaces that are implemented in different classes extend the public scope of each class by the same set of components. • If a class does not have any class-specific public components, the interfaces define the entire public face of the class.
- Slides: 11