Está en la página 1de 78

Object Oriented ABAP

Thursday, November 8,
2018

Vertrauliches Dokument – für interne Nutzung Copyright ©EBS Romania S.R.L. 1


Workshop Goals

This workshop will enable you to:

• Learn object – oriented concepts.


• Learn the principle of object - oriented programming.
• Learn the application of object - oriented ABAP.

Vertrauliches Dokument – für interne Nutzung Copyright ©EBS Romania S.R.L. 2


Course Contents

Object Oriented Concepts


Object Oriented Programming.
Advantages of the Object-Oriented Approach

Classes Methods
Components of a Class • Syntax and Visibility
• Instance Methods and Static Methods
• Constructor

Attributes Reference Variables


• Syntax and Visibility • Creating References
• Instance Attributes and Static Attributes • Assigning References
Course Contents

Inheritance
 Super classes and Subclasses
 Visibility
 Inheritance and the (Instance) Constructor
 Parameters
 Redefining Methods in OOABAP
 Compatibility
 Principles of the Narrowing Cast
 Static and Dynamic Components
 Final Classes and Methods
Course Contents

Polymorphism
 Advantages Compared to Procedural Programming
 Abstract Classes and Methods
 Component Namespaces in Classes

Interfaces
 Defining and Implementing an Interface
 Working with Interface Components
 Interface References
• Narrowing Cast
• Widening Cast
 Using Several Interfaces
 Polymorphism and Interfaces
 Polymorphism and Inheritance
 Compound Interfaces
Course Contents

Events
 Define and Trigger Events
 Handle Events
 Register and deregister Events
 Receive a reference from Sender
Object – Oriented Concepts

What are Objects ?


OOPS

Object Oriented Programming

• Encapsulation

• Inheritance

• Polymorphism

• Instantiation

• Interfacing

• Events
Advantages

• Simplicity

• Explicitness

• Maintainability

• Future Orientation
Classes

• Classes are the central element of object-orientation.

• A Class is an abstract description of an object.

• Classes are templates for objects.

• The attributes of objects are defined by the components of the class,


which describe the state and behavior of objects.
Classes

• You define global classes and interfaces in the Class Builder


(Transaction SE24) in the ABAP Workbench.

• They are stored centrally in class pools in the class library in the R/3
Repository.

• All of the ABAP programs in an R/3 System can access the global
classes.
Components in a class
Classes

Defining Local Classes

• A complete class definition consists of a declaration part and, if required, an implementation


part.

• The declaration part of a class <class> is a statement block:


CLASS c1 DEFINITION.
….
ENDCLASS.

• If you declare methods in the declaration part of a class, you must also write an
implementation part for it.
CLASS c1 IMPLEMENTATION.
….
ENDCLASS.
Attributes
Classes

Defining Local Classes


Attributes, Types, Constants - Syntax
Attributes and Visibility
Instance attributes and Static attributes
Methods
Methods : Syntax
Methods and Visibility
Instance methods and Static methods
Instance methods and Static methods : Example
Constructor
Constructor : Example
Static Constructor : Implementation
Static Constructor : Call Examples
Creating Objects
Reference Variables
Creating Objects : Syntax
Assigning References
Abstract and Final Classes and Methods
Abstract Classes and Methods
CLASS c1 DEFINTION ABSTRACT. CLASS c1 IMPLEMENTATION.
METHODS: m1, METHODS m1.
m2 ABSTRACT. ENDMETHOD.
ENDCLASS. ENDCLASS.

 Abstract classes can‘t be instantiated


 Abstract methods can only be implemented in a subclass, using
REDEFINITION.
 A class containing an abstract method must itself be abstract.
 Compared to interfaces, abstract classes can be partly implemented but
are restricted by single inheritance.
Abstract and Final Classes and Methods
Final Classes and Methods

CLASS c1 DEFINTION FINAL. CLASS c2 DEFINITION.


METHODS: m1, METHODS: m1,
m2. m2 FINAL.
ENDCLASS. ENDCLASS.

 Final classes can‘t have subclasses.


 Final methods can‘t be redefined.
 All methods of a final class are implicitly final.
 Final classes are endnodes of the inheritance tree.
Inheritance

• Inheritance allows you to derive a new class from an existing class.


• You do this using the INHERITING FROM addition in the
CLASS <subclass> DEFINITION INHERITING FROM <superclass>
statement.
• The new class <subclass> inherits all of the components of the existing class
<superclass>.
• The new class is called the subclass of the class from which it is derived.
• The original class is called the superclass of the new class.
Inheritance
Inheritance : Syntax
Relationships between super classes and subclasses

Relationships between super classes and subclasses


Inheritance and Visibility
Inheritance and (Instance) constructor
Parameters and CREATE OBJECT
Redefining Methods in ABAP Objects
Redefining Methods : Example
Compatibility and Narrowing Cast
Principles of the Narrowing Cast
Static and Dynamic Types: Example
Static and Dynamic Types for References
Static and Dynamic Types for References

Widening the Cast


Widening the cast
Polymorphism
Polymorphism
Polymorphism
Interface

• Interfaces exclusively describe the external point of contact


of a class, but they do not contain their own
implementation part.
Interfaces – Implementation
New additions for implementation in classes:
6.10

CLASS class DEFINITION.


PUBLIC SECTION.
INTERFACES intf
intf.
[ABSTRACT METHODS meth ...]
[FINAL METHODS meth ...]
[ALL METHODS ABSTRACT|FINAL]
[DATA VALUES attr = value ...].
...

 You can make interface methods abstract or final.


 You can assign start values to interface attributes.
Composing Interfaces

Composing Interfaces

INTERFACE i1.
...
INTERFACES i2, i3, ...
...
...
ENDINTERFACE.

 i1: Compound Interface


 i2, i3, ... : Component Interfaces
Composing Interfaces – Naming

INTERFACE i2.
INTERFACES i1.
ENDINTERFACE.

INTERFACE i3.
INTERFACES i1, i2.
ENDINTERFACE.

 i3 contains i1 exactly once

 The name of i1 in i3 is i3~i1


 No nesting of names (i3~i2~i1) allowed
Composing Interfaces – Diamond Inheritance

INTERFACE i1.
METHODS meth.
ENDINTERFACE.

INTERFACE i2. INTERFACE i3.


INTERFACES i1. INTERFACES i1.
METHODS meth. METHODS meth.
ENDINTERFACE. ENDINTERFACE.

INTERFACE i4.
INTERFACES i2, i3.
ENDINTERFACE.
Problem?
Composing Interfaces – Implementation
No Problem! CLASS c1 DEFINITION.
PUBLIC SECTION.
INTERFACES i4.
 Each interface is implemented ENDCLASS.
once.
CLASS c1 IMPLEMENTATION.
 All interfaces are implemented METHOD i1~meth.
at the same level. ...
ENDMETHOD.
 Each interface component is METHOD i2~meth.
unique. ...
ENDMETHOD.
METHOD i3~meth.
...
ENDMETHOD.
ENDCLASS.
Composing Interfaces – Aliases
INTERFACE i1.
METHODS m1.  Inside: Access to deep components
ENDINTERFACE. of compound interfaces via aliases
INTERFACE i2. only.
INTERFACES i1.  Outside: Narrowing cast.
ALIASES m2 FOR i1~m1.
ENDINTERFACE.

INTERFACE i3.
INTERFACES i2.
ALIASES m3 FOR i2~m2. FOR i2~i1~m1.
ENDINTERFACE.

DATA: iref1 TYPE REF TO i1,


iref3 TYPE REF TO i3.
iref1 = iref3.
iref1->m1( ). iref3->i2~i1~m1( ).
iref3->m3( ).
Defining and Implementing Interface
Working with Interface components
Interface References Narrowing casting
Interface

• The assignment of an object reference to an interface reference is


known as a narrowing cast since, as with inheritance, only a part of the
object interface is visible once you have assigned the reference.

• With an interface reference, you can no longer address all components


in the class carrying out the implementation, but only the components
defined in the interface.
Interface references widening cast
Interface

• The widening cast is, as with inheritance, the opposite of the narrowing
cast: here it is used to retrieve an object reference from an interface
reference. Obviously it cannot be statically checked, since an interface can
be implemented by more than one class.

• An object reference cannot be assigned to an interface reference if it has


itself not implemented the corresponding interface.
Using several Interface

• In the above example, one class is implementing several interfaces. Even if these
interfaces contain components with the same name, they are differentiated in the
class carrying out the implementation by the prefix “<interfacename>~”.
Polymorphism and Interface
Events

• Objects or Classes use events to trigger Event Handler methods in other objects or
classes.
• When an event is triggered any number of Event Handler Methods can be called.
• The events of a class can be raised in the same class using the RAISE EVENT Statement.
• Events have only output parameters which are accepted by the Event Handler
Methods as input parameters.
• The link between the trigger and the handler is established dynamically at runtime
using the statement SET HANDLER.
Events Overview
Triggering and handling Events : Overview
Defining and Triggering Events
Handling and Registering Events
Handling Events
Registering for an Event : Syntax
Deregistration
Registration/Deregistration : Handler Table
Event handling : Characteristics
Events and Visibility
Thank you for you attention

78

También podría gustarte