ABAP Objects – Class and objects

What is Object Oriented Programming?

Object oriented programming tries to map the real world objects with their attributes and methods as realistically as possible in program constructs. The aim is to create the program flow in such a way as if it is taking place in the real world. This makes the interaction between the developer and the user easier. The user can formulate business requirements on a program in the language in which the user is familiar with and the developer can convert these directly instead of the classic programming model.

In the object-oriented programming model, a different approach is used. Attributes and methods which belong to a specific object are grouped together and made accessible to the user using a well-defined interface behind which the actual implementation details are hidden. so, the attributes of the object are only changed by the methods of the object and not directly by the user of and object. This ensures that the status of the object is always consistent.

When do you call an object an object oriented programming? if it has these features.

  • Abstraction: Abstraction refers to the ability to reflect the real world  processes as realistically as possible through programming. These processes can be a business or a technical nature.
  • Encapsulation: Implementation details are hidden behind well-defined an documented interfaces. They ensure that the abstract representation of an object is used only in accordance with its specification.
  • Inheritance: New abstractions i.e. classes are derived from existing ones. They inherit all the attributes and methods of the higher class and can expand and specialize them.
  • Polymorphism: Different objects can present the same interface to the outside. A user only needs to know the interface and need not know every minute detail of the classes.

Why do you need ABAP Objects?

ABAP Objects oriented programming lets you be at par with the others as this is better methodology, like any other programming language which has its pros and cons. To understand the recent concept of ABAP ,such as BAPI,  BADI,  WORKFLOW etc. For integration of ABAP with Microsoft Technologies and JAVA as these are built on OOPS concept. To exploit the class resources that has been provided by   SAP.

What are ABAP Objects all about?

Class and objects

The main objective of ABAP Objects is to model real world entity · Each real world entity can be modeled by its characteristics and functionality · The real world entity when modeled into OOP world is known as Class, characteristics as attributes and functionality as methods.
Classes can be of two types
  1. Global Class: Global Classes can be accessed in any program. These classes can be accessed from anywhere and are  stored in the class repository and this is created using SE24 and should start with Z or Y.
  2. Local Class:  Local Classes are limited to the program in which they are defined. These classes can be accessed from the program in which they are created and it can have any namespace.

Class Declaration in ABAP Objects

A Class declaration would contain two parts. 1) Definition 2) Implementation

CLASS test DEFINITION.
{class definition is done here}
ENDCLASS.
 
CLASS test IMPLEMENTATION.
 
{method implementation is done here}
ENDCLASS.

The above statements would define a local class.

Objects in ABAP Objects

Objects are an instance of a Class or in other words, they are runtime entities. We can create any number of objects for a class.

Creating and accessing Objects.

Data: obj type ref to test.
Create object obj.
WRITE : OBJ->ATTRIBUTE1.            (i.e. object name->attribute name)

Components of Class in ABAP Objects

DATA – for instance attributes
METHODS – for instance methods
EVENTS – for instance events
Static components:
CLASS-DATA – for static attributes
CLASS-METHODS – for static methods
CLASS-EVENTS – for static events
CONSTANTS – for constants (that is, constant static attributes)
TYPES – for internal types in classes

All of the components should belong to visibility section. Components can be PUBLIC, PROTECTED & PRIVATE.

The declaration part of the class is divided accordingly into up to three sections:
PUBLIC SECTION
PROTECTED SECTION
PRIVATE SECTION

Note. Tere is no default visibility section in a class. This sequence of visibility must be maintained in a class.

PUBLIC SECTION
All components defined in this section are visible in the subsequent protected and private sections. These are also externally visible.

PROTECTED SECTION
All components in this section can access the components from the public section. In turn, they can be accessed by the components of the private section.
These components are externally visible only to the inherited class.

PRIVATE SECTION
All components of this section can access the components of the public and private sections. They are not externally visible.

CLASS test DEFINITION.
PUBLIC SECTION.
METHODS: display.
DATA: stat TYPE i.
PRIVATE SECTION.
DATA: newint TYPE i.
ENDCLASS.
 
CLASS test IMPLEMENTATION.
METHOD display.
WRITE: / stat.
ENDMETHOD. "display
ENDCLASS.
 
DATA: obj TYPE REF TO test.
 
START-OF-SELECTION.
 
CREATE OBJECT obj.
CALL METHOD obj->display.
obj->stat = 456.

Object oriented ABAP programming is very vast and these OOABAP FAQs can give you a quick start.

Comments