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.
What are ABAP Objects all about?
Class and objects
- 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.
- 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.
Redefining Method in ABAP Object Class
A Redefined method is a method in a subclass that provides a new definition of an inherited method from a super class in order to provide a more specialized implementation of the subclass.
The Redefined method will have the same name and interface, but will have a new implementation.
Redefined methods cannot be defined as final methods in the super class. Defining a method as final indicates that the method cannot be overridden or redefined.
A constructor method cannot be redefined as it is implicitly a final method.
The method declaration and implementation in the super class is not affected. The redefined method in the subclass “hides” the original method in the super class. All references to an object in the subclass will use the redefined method. The system checks for methods first in the current class and then ‘up the hierarchy’.
Only an inherited public or protected instance method can be redefined. Static Methods cannot be redefined.
Visibility of OOABAP Components
in OOABAP, Each class component has a visibility. The whole class definition is separated into three visibility sections:
- PUBLIC : Data declared in public section can be accessed by the class itself, by its subclasses as well as by other users outside the class.
- PROTECTED :Â Data declared in the protected section can be accessed by the class itself, and also by its subclasses but not by external users outside the class.
- PRIVATE: Data declared in the private section can be accessed by the class only, but not by its subclasses and by external users outside the class.
CLASS DEFINITION. PUBLIC SECTION: CLASS-METHODS :.... CLASS-DATA:... METHODS:.... DATA:.... PROTECTED SECTION: METHODS:.... DATA:.... PRIVATE SECTION: METHODS:.... DATA:.... ENDCLASS. |