Upload
api-3834175
View
1.128
Download
18
Embed Size (px)
Citation preview
Object Oriented ABAP
Introducing ABAP Objects
2 The ASPplus Solutions Company
Introduction to ABAP Objects
• ABAP Objects, the object-oriented (OO) extension to the ABAP language, is available with releases 4.5 and 4.6. Release 4.6 adds inheritance, nested interfaces, and dynamic method invocation. In this training, we’ll take you through an introduction to ABAP Objects and give you a comprehensive example (The code for the example can be tested and debugged in DEMO_ABAP_OBJECTS program in R/3 release 4.6 or higher).
3 The ASPplus Solutions Company
What are ABAP Objects ?
• ABAP objects are compatible extension of the existing ABAP language.
• You can use existing ABAP statements within ABAP objects.
• You can use ABAP objects within existing programs.
• ABAP objects are fully integrated in ABAP debugger.
4 The ASPplus Solutions Company
Continued….
• The term has two meanings
• On one hand, it stands for the entire ABAP runtime environment.
• On the other hand, it represents the object oriented extension of the ABAP language.
5 The ASPplus Solutions Company
ABAP objects are created because
• The Business Object Repository(BOR) already presented an OO view of the system to the outside, and we wanted a common programming model for both outside and inside.
• We wanted seamless integration with external object modules (DCOM and CORBA).
6 The ASPplus Solutions Company
Continued…
• We wanted the potential benefits of objects with in R/3, including better control of complexity, a better means for encapsulation, extensibility, and reuse, and a language basis for patterns and frameworks.
• We needed objects to be the foundation for a new GUI Programming model.
7 The ASPplus Solutions Company
Design Goals
• To make ABAP Objects as simple as possible.
• To use only proven OO concepts.
• To comply with external standards whenever possible (for example, to allow mapping to COM, CORBA, and UML).
8 The ASPplus Solutions Company
Continued...
• To require much stronger typing (type checking) than traditional ABAP, where type specification is almost always optional.
9 The ASPplus Solutions Company
Elements Of ABAP Objects
• Classes
• Interfaces
• Objects
• Object References
• Inheritance
10
The ASPplus Solutions Company
Classes• Classes are the central element of object-orientation• A class describes a general element or a general
concept, for example the abstract concepts Business Partner, Material, Transaction, Equipment or list.
• You can define classes globally using the class builder (Transaction SE24) or locally in an ABAP program.
• All of the ABAP programs can access the global classes.
• Local classes and interfaces can only be used in the program in which they are defined.
11
The ASPplus Solutions Company
Components of classes
• Attributes
• Methods
• Events
• Types and Constants
12
The ASPplus Solutions Company
Continued…
• In ABAP Objects classes are made up of a definition and an implementation part.
• Example
CLASS C1_PARTY DEFINITION.
ENDCLASS.
CLASS C1_PARTY IMPLEMETAION.
ENDCLASS.
13
The ASPplus Solutions Company
Attributes
• Attributes can take on values within an object at runtime. The sum of all attributes and values describes the state of an object.
• Attributes can be of two types
1. Static attributes
2. Instance attributes
14
The ASPplus Solutions Company
Instance Attributes
• Instance attributes represents the instance-dependent status of an object. Instance attributes are declared using the statement DATA
15
The ASPplus Solutions Company
Static Attributes
• Static attributes represents the instance-independent status of the class, which applies to all instances. Static attributes exist once for each class. They are declared using the statement CLASS-DATA and are retained for the entire runtime.
16
The ASPplus Solutions Company
Example
Class C1_party DEFINITION.
PUBLIC SECTION.
CLASS-DATA : instance_count type I.
DATA : name(50) type C.
ENDCLASS.
17
The ASPplus Solutions Company
Methods
• Methods describe the behavior of objects within a class.
• They could be compared to the subroutine in normal ABAP
• They can access all attributes of their class and can thus change the status of an object.
18
The ASPplus Solutions Company
Continued…
• Again Methods are of two type
Static Methods
Instance Methods
• Special Method : Constructor
19
The ASPplus Solutions Company
Methods
• An instance method is declared using the statement METHODS. It can access all attributes of a class and trigger all events of the class.
• Static method is declared using the statement CLASS-METHODS. It can access static attributes and trigger only static events.
20
The ASPplus Solutions Company
Methods
• Methods can have any number of IMPORTING parameters and one RETURNING parameter.
• In addition to the regular methods, there are special methods called CONSTRUCTOR and CLASS_CONSTRUCTOR, which are called implicitly when objects are generated or when class components are called for the first time.
21
The ASPplus Solutions Company
Example
Types : Boolean(1) type c, Pname(50) type c.CLASS C1_party DEFINITION.
PUBLIC SECTION.CLASS-METHODS : is_class_with_objects RETURNING VALUE(re_bool) TYPE bollean.METHODS : change_name IMPORTING value (im_new_name) TYPE pname.
ENDCLASS
22
The ASPplus Solutions Company
Events
• Events enable objects or classes to trigger event handler methods in other objects or classes. They are declared in the declaration part of a class and raised in its methods.
• They are of two typesInstance EventsStatic Events
23
The ASPplus Solutions Company
Continued…
• You can declare events by using the statements EVENTS for instance events and CLASS-EVENTS for static events.
• Instance events can be raised in any method of the declaring class, whereas static events can only be raised in static methods.
24
The ASPplus Solutions Company
ExampleClass ship DEFINITION.
PUBLIC SECTION.METHODS DRIVE.EVENTS emergency_call.
ENCLASS.CLASS ship IMPLEMENTATION.
METHOD DRIVE. if speed > more_speed.
RAISE EVENT emergency_call.Endif.ENDMETHOD.
ENDCLASS.
25
The ASPplus Solutions Company
Types and Constants.
• The statements TYPES can be used to declare self-defined ABAP data types within a class.
• Constants are specific static attributes whose values are defined in the declaration and cannot be changed. Constants, are not instance-dependent and exist ones for all objects of a class.
26
The ASPplus Solutions Company
Interface
• The Interface concept describes a class interface. You can define the same components in an interface that you can in classes, however without implementing them.
• Classes can implement interfaces, and subsequently be addressed via these interfaces. For example, a class cl_class can implement the interface if_archive and thereby become archivable, or it can implement if_workflow and thereby take part in a workflow.
27
The ASPplus Solutions Company
Nested Interfaces
• By using the INTERFACES statement within the interface definition, you can define nested interfaces.
28
The ASPplus Solutions Company
Interface References
• A class which implements a specific interface can be addressed via this interface reference. Using such an interface reference, you can access the components defined in the interface. In this way a user can view different classes through the 'spectacles' of an interface and address them in a uniform manner. (DATA: reference TYPE REF TO cl_interface).
29
The ASPplus Solutions Company
Inheritance
• Inheritance allows you to define classes by deriving them from existing classes. The new class adopts or inherits all components of the existing class. The existing class is called a super class of the new class.
• A class can have any number of direct subclasses, but only one super class. ABAP Objects thus uses single inheritance.
30
The ASPplus Solutions Company
Objects
• Objects are instance of classes. All transient objects exist within the context of an internal session (memory area of an ABAP program). You can create any number of objects(instances) for a class. Each one has a unique identity and its own attributes.
• An object remains intact for as long as it is used in a program.
31
The ASPplus Solutions Company
Object References
• Object references are used as pointer to objects. Object references (the value of reference variables) are the only way to access component of an object in an ABAP program.You can use references to access attributes and methods, but not events.
32
The ASPplus Solutions Company
THANK YOU