sap abap object oriented language

14
SAP ABAP Object Oriented Language Object Orientation Object orientation (or, more correctly, object-oriented programming) is a problem-solving method that represents the real world in a series of software objects. Object-oriented programming is based on a programming model in which data and functions are unified in objects. The remaining language scope of ABAP mainly supports procedural programming, where the data is stored at other places than the objects and where programs that are modularized by procedures access this data. This document defines a few general terms that are widely used in object orientation and in ABAP Objects. Objects Objects represent abstract or concrete objects of the real world. An object is a section of program code that has data (called attributes) and provides services called methods (sometimes also known as operations or functions). Methods typically work with private data in the object (attributes, also known as the object state), that are only visible within the object. This guarantees the internal consistency of the object, since the data is only changed by the methods, not by the user. This ensures that the object is consistent in itself. Classes Classes are program code that describes objects. Technically, an object is an instance of a class. In theory, you can create an infinite number of objects from a single class definition. Each instance of a class (object) has its own values for its attributes. Specifies “Status Data”/No of Characteristic

Upload: irfanusa

Post on 20-Jan-2016

41 views

Category:

Documents


1 download

DESCRIPTION

Basic guide for SAP ABAP language

TRANSCRIPT

Page 1: SAP ABAP Object Oriented Language

SAP ABAP Object Oriented Language

Object Orientation

Object orientation (or, more correctly, object-oriented programming) is a problem-solving method that represents the real world in a series of software objects.

Object-oriented programming is based on a programming model in which data and functions are unified in objects. The remaining language scope of ABAP mainly supports procedural programming, where the data is stored at other places than the objects and where programs that are modularized by procedures access this data.

This document defines a few general terms that are widely used in object orientation and in ABAP Objects.

Objects

Objects represent abstract or concrete objects of the real world. An object is a section of program code that has data (called attributes) and provides services called methods (sometimes also known as operations or functions). Methods typically work with private data in the object (attributes, also known as the object state), that are only visible within the object. This guarantees the internal consistency of the object, since the data is only changed by the methods, not by the user. This ensures that the object is consistent in itself.

Classes

Classes are program code that describes objects. Technically, an object is an instance of a class. In theory, you can create an infinite number of objects from a single class definition. Each instance of a class (object) has its own values for its attributes.

CLASS OBJECTS

Attributes(Data)

Methods(Functions)

Specifies “Status Data”/No of Characteristic

Instances

Behavior -

Page 2: SAP ABAP Object Oriented Language

Object References

In a program, you identify and address objects using a unique object reference. They allow you to access the attributes and methods of an object.

In object-oriented programming, objects usually have the following characteristics:

Encapsulation

Objects restrict the external visibility of their resources (attributes and methods). Each object has an interface that determines how other objects or applications can use it. The implementation of the object is encapsulated (not visible outside the class).

Polymorphism

Methods with the same name can behave differently in different classes. In object-oriented programming, you can use interfaces to address methods with the same name in different objects. The form of address always remains the same, but the actual method implementation is class-specific, and can be different in each class.

Inheritance

You can derive a class from another class. A derived class (subclass) inherits the data and methods of its superclass. You can add new methods to a subclass, or redefine existing

Page 3: SAP ABAP Object Oriented Language

methods. Redefined methods have the same name and interface as the original method. Their classes are therefore polymorphous, too.

Benefits of Object Orientation

Object orientation has the following advantages:

Complex software systems become easier to understand, since an object-oriented architecture resembles reality more closely than other programming techniques.

Changes in object-oriented systems should be possible locally (at class level), without further changes being necessary in other parts of the system. This reduces the amount of maintenance required.

Polymorphism and inheritance enable many individual components to be reused.

Object-oriented systems require fewer revisions and less maintenance, because the majority of problems can be discovered and corrected in the design and development phases.

Achieving these goals requires:

Object-oriented programming languages

Object-oriented programming techniques do not necessarily require object-oriented programming languages. However, they do depend on the implementation of object-oriented constructions in the system kernel.

Object-oriented tools

Object-oriented tools help you create object-oriented programs in object-oriented languages. They allow you to store and visualize your program objects and the relationship between them.

Object-oriented modeling

Object-oriented modeling of a software system is the most important, most time consuming, and most difficult task required to achieve the above goals. Object-oriented design encompasses more than just object-oriented programming, and offers logical advantages that are independent of the eventual implementation.

Page 4: SAP ABAP Object Oriented Language

Attributes:

Attributes contain the data that can be stored in the objects of a class.

Class attributes can be one of three types: Elementary (i_lifnr TYPE lfb1-lifnr,), structured (Reference to a structure), or Table-type (i_lfa1 TYPE STANDARD TABLE OF lfa1.).

They can consist of local or global data types or reference types.

Examples of attributes for the class LCL_VEHICLE are:

Page 5: SAP ABAP Object Oriented Language

In classes, you can only use the TYPE addition to refer to data types. You can only use the LIKE reference for local data objects.

The READ-ONLY addition means that a public attribute that was declared with DATA can be read from outside, but can only be changed by methods in the same class.You can currently only use the READ-ONLY addition in the public visibility section (PUBLIC SECTION) of a class declaration or in an interface definition.With TYPE REF TO, an attribute can be typed as any reference. This will be discussed in more detail later.

Page 6: SAP ABAP Object Oriented Language

Private and Public Attributes of a Class:

Define private attributes in the PRIVATE SECTION of a class. Define public attributes in the PUBLIC SECTION.It is syntactically impossible to access private

Public Attributes Private Attributes

You can protect attributes against access from outside by characterizing them as private attributes. The private components of the class cannot be addressed directly from outside. They are not visible to the outside user.

Attributes that an outside user can access directly are public attributes. The public components of a class are sometimes collectively known as the class's interface.

Using the private visibility section is also known as information hiding or encapsulation. In part, this is to protect the user of a class: Assume that the private components of a class are changed at some point, but its interface remains the same.

All external users can only access their components through the interface of the class, and so can continue to work with the class as usual after the change is made. The user does not notice the change. Only the internal implementation was changed.

Conversely, if the public components of a class were incompatibly changed, every outside user would have to take these changes into account. You should therefore use public attributes very sparingly, or avoid making subsequent incompatible changes to the public components of your classes altogether.

Page 7: SAP ABAP Object Oriented Language

Instance Attributes:

Static Attributes:

Page 8: SAP ABAP Object Oriented Language

Syntax of Methods:

Methods are internal procedures in classes that determine the behavior of the objects. They can access all attributes in their class and can therefore change the state of other elements.

Methods have a signature (interface parameters and exceptions) that enables them to receive values when they are called and pass values back to the calling program. Methods can have any number of IMPORTING, EXPORTING, and CHANGING parameters. All parameters can be passed by value or reference.

Instance Public Methods:

Methods also have to be assigned to a visibility section. This determines whether the methods are called from outside the class or only from within the class. You define PUBLIC MEHTODS in the Public Section of the class.

Instance Private Methods:

You define private methods in the PRIVATE SECTION of a class. You define public attributes in the PUBLIC SECTION.

Page 9: SAP ABAP Object Oriented Language

It is not possible to directly access private methods from outside. It is not possible to directly access private methods from outside. However, a private method can be called by a public method.

CLASS lcl_vehicle DEFINITION.

Calling Private methods by a Public Method

It is not possible to directly access private methods from outside. However, a private method can be called by a public method.In this example, INIT_TYPE is a private method that is called by the public method SET_TYPE. The instruction to initialize the attributes could exist in other contexts as well, so the definition of this private “auxiliary method “is useful.

STATIC Methods

Static methods are defined at class level. The restriction that only static components can be accessed applies in the implementation part. This means that static methods do not need instances, that is, they can be accessed directly through the class. The methods are defined using the syntax keyword CLASS-METHODS.

CLASS-METHODS:display_n_o_airplanes.

Page 10: SAP ABAP Object Oriented Language

Reference Variables

DATA r_vehicle1 TYPE REF TO lcl_vehicle is used to define a reference variable, which is thereby typed as a pointer to objects of type lcl_vehicle. The null reference is the technical initial value of a reference variable. (The pointer is pointing to nothing.)

Page 11: SAP ABAP Object Oriented Language

Objects: Create Objects

Page 12: SAP ABAP Object Oriented Language

Creating Reference to main class:

TYPES: tt_conditions TYPE STANDARD TABLE OF konv.DATA: lv_salesdoc TYPE vbak-vbeln,      lo_salesdoc TYPE REF TO zcl_salesdoc,      lv_itemno   TYPE vbap-posnr,      lv_condtype TYPE konv-kschl,      lv_condval  TYPE konv-kwert,      lv_currency TYPE vbak-waerk,      lt_conditions TYPE tt_conditions.

Creating Object:

IF NOT lv_salesdoc IS INITIAL.  swc_get_element container 'i_Salesdoc' lv_salesdoc.  CREATE OBJECT lo_salesdoc    EXPORTING      i_salesdoc        = lv_salesdoc*    i_item_no         = 000000      i_condtype        = lv_condtype      e_conditions      = lt_conditions      e_condvalue       = lv_condval*  EXCEPTIONS*    errorwithinstance = 1*    others            = 2      .  IF sy-subrc <> 0.* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.  ENDIF.

Muhammad Shokat, 02/27/14,
Must refer to your main CLASS
Muhammad Shokat, 02/27/14,
We can create object as lo_salesdoc is referred to main class in above.