Wednesday, 22 February 2017

OO ABAP Basics

To understand about the Classes and Objects, lets have a quck look at these concepts.
 CLASS:
 Structure of a Class:
1.     A class contains components.
2.     Each component is assigned to a visibility section.
3.     Classes implement methods.
 
 Defining Classes (Local):
The declaration part of a class <class> is a statement block:
CLASS <class> DEFINITION.
...                       declaration for all components (attributes, methods, events) of the class
ENDCLASS.
 CLASS <class> IMPLEMENTATION.
...          contains the implementation of all methods of the class
ENDCLASS.
 Visibility Sections
Public Section:  All of the components declared in the public section are accessible to all users of the class, and to the methods of the class and any classes that inherit from it. The public components of the class form the interface between the class and its users.
 Protected Section: All of the components declared in the protected section are accessible to all methods of the class and of classes that inherit from it. Protected components form a special interface between a class and its subclasses.

Private Section: Components that you declare in the private section are only visible in the methods of the same class. The private components are not part of the external interface of the class.
 OBJECTS
Objects are instances of classes. Each object has a unique identity and its own attributes. To access an object from an ABAP program, you use object references. Object references are pointers to objects.
 Reference variables contain references. A reference variable is either initial or contains a reference to an existing object. The identity of an object depends on its reference. A reference variable that points to an object knows the identity of that object. Users cannot access the identity of the object directly.
 There are two principal types of references: 
Class references and interface references.
 You define class references using the
... TYPE REF TO <class>
 addition in the TYPES or DATA statement, where <class> refers to a class. A reference variable with the type class reference is called a class reference variable.
 A class reference <cref> allows a user to create an instance (object) of the corresponding class, and to address a visible component <comp> within it using the form
cref->comp
 Create Object : Once you have declared a class reference variable <obj> for a class <class>, you can create an object using the statement
 CREATE OBJECT <cref>.
 Addressing the Object Components
Programs can only access the instance components of an object using references in reference variables. The syntax is as follows (where <ref> is a reference variable):
§         To access an attribute <attr>: <ref>-><attr>
§         To call a method <meth>: CALL METHOD <ref>-><meth>
 You can access static components using the class name as well as the reference variable. It is also possible to address the static components of a class before an object has been created.
§         Addressing a static attribute <attr>: <class>=><attr>
§         Calling a static method <meth>: CALL METHOD <class>=><meth>
 Within a class, you can use the self-reference ME to access the individual components:
§         To access an attribute <attr> in the same class: ME-><attr>
§         To call a method <meth> in the same class: CALL METHOD ME-><meth>
  Events
Triggering and handling an event means that certain methods act as triggers and trigger events, to which other methods - the handlers - react. This means that the handler methods are executed when the event occurs.
 To trigger an event, a class must
1.      Declare the event in its declaration part.
2.      Trigger the event in one of its methods.
 To handle an event, a method must
1.      be defined as an event handler method for that event.
2.      be registered at runtime for the event.
 *Registering Event Handler Methods :*To allow an event handler method to react to an event, you must determine at runtime the trigger to which it is to react. It links a list of handler methods with corresponding trigger methods.
 There are four different types of event.
1.      An instance event declared in a class.
2.      An instance event declared in an interface.
3.      A static event declared in a class.
4.      A static event declared in an interface.

No comments:

Post a Comment