32
Object Oriented ABAP Introducing ABAP Objects

Introducing Abap Objects

Embed Size (px)

Citation preview

Page 1: Introducing Abap Objects

Object Oriented ABAP

Introducing ABAP Objects

Page 2: 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).

Page 3: Introducing Abap Objects

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.

Page 4: Introducing Abap Objects

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.

Page 5: Introducing Abap Objects

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).

Page 6: Introducing Abap Objects

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.

Page 7: Introducing Abap Objects

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).

Page 8: Introducing Abap Objects

8 The ASPplus Solutions Company

Continued...

• To require much stronger typing (type checking) than traditional ABAP, where type specification is almost always optional.

Page 9: Introducing Abap Objects

9 The ASPplus Solutions Company

Elements Of ABAP Objects

• Classes

• Interfaces

• Objects

• Object References

• Inheritance

Page 10: Introducing Abap Objects

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.

Page 11: Introducing Abap Objects

11

The ASPplus Solutions Company

Components of classes

• Attributes

• Methods

• Events

• Types and Constants

Page 12: Introducing Abap Objects

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.

Page 13: Introducing Abap Objects

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

Page 14: Introducing Abap Objects

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

Page 15: Introducing Abap Objects

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.

Page 16: Introducing Abap Objects

16

The ASPplus Solutions Company

Example

Class C1_party DEFINITION.

PUBLIC SECTION.

CLASS-DATA : instance_count type I.

DATA : name(50) type C.

ENDCLASS.

Page 17: Introducing Abap Objects

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.

Page 18: Introducing Abap Objects

18

The ASPplus Solutions Company

Continued…

• Again Methods are of two type

Static Methods

Instance Methods

• Special Method : Constructor

Page 19: Introducing Abap Objects

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.

Page 20: Introducing Abap Objects

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.

Page 21: Introducing Abap Objects

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

Page 22: Introducing Abap Objects

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

Page 23: Introducing Abap Objects

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.

Page 24: Introducing Abap Objects

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.

Page 25: Introducing Abap Objects

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.

Page 26: Introducing Abap Objects

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.

Page 27: Introducing Abap Objects

27

The ASPplus Solutions Company

Nested Interfaces

• By using the INTERFACES statement within the interface definition, you can define nested interfaces.

Page 28: Introducing Abap Objects

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).

Page 29: Introducing Abap Objects

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.

Page 30: Introducing Abap Objects

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.

Page 31: Introducing Abap Objects

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.

Page 32: Introducing Abap Objects

32

The ASPplus Solutions Company

THANK YOU