Sunday, August 21, 2011

INHERITANCE & POLYMORPHISM


Inheritance defines the relationship between classes, in which a class (subclass) uses the structure and behavior that has already been defined in one or more other classes (superclasses). So simply this means "Inheritance is about reuse!".
Allow me to use a concrete example to explain inheritance: Collection.
A collection is any number of objects (more precisely object references).  However, there could be many types of collection. Therefore, I will implement each type of collection as a class. In principle this approach is correct. However, you will soon establish that all collections have several components in common like:
1.                Each class requires a method in order to add objects to a collection.
2.                Each class requires a method in order to delete objects from a collection.
3.                Each class has a method which identifies the number of object references in the collection and so on.
Inheritance is the solution to this situation. You implement all of the similarities in the class which is Superclass. You then implement the individual types of collection in their own classes which are Subclassesof the Superclass. As a subclass, these classes inherit all of the components of the Superclass. Attributes, methods and events are inherited. In addition, you can implement additional attributes, methods and events in the subclass.
POLYMORPHISM
Polymorphism occurs, where classes implement the same functionality with different methods (one functionality, several methods but the same name). This can occur via an inheritance relationship, in that the methods belonging to the superclass are redefined in the subclasses and implemented differently. ABAP Objects requires the method names to be the same and the signature to be the same (signature = method interface).
Polymorphism can be achieved in 2 ways:
(1) Two independent classes implement methods with the same names and the same signature with the intention, that the methods should be called dynamically from a third location.
(2) A superclass implements a method, and in a subclass you want to re-implement the same method, as the superclass implementation is not suitable for the subclass.
The first scenario will not occur very often in ABAP Objects, as the interface concept was created precisely for such cases.

EVENTS


Events are recognized in particular by programming interfaces of the GUIs (Windows, Motif, etc.), for example, you can "ask" the GUI to trigger an event if the user moves the mouse over a specific part of the screen. When the event occurs you are telling the GUI to change the shape of the mouse pointer.
Events allow for the loose coupling of components (classes or objects) in a system. The event trigger does not normally know at the time of coding who is going to react to the event. Those components, which want to react to the event, register at the event runtime, in that they tell the runtime environment which method is to be executed when the event is raised. In this way many components can register for an event.
Event handler methods can proceed synchronously as well as asynchronously. At present, ABAP Objects only supports synchronous calling of the event handler method.
Code listing for: Z_006_EVENT
Description: EXAMPLE OF EVENTS
*---------------------------------------------------------------
THIS EXAMPLE SHOWS THE USE OF EVENTS.
*---------------------------------------------------------------
*
* The event trigger does not normally know at the time of
* coding who is going to react to the event. Those components,
* which want to react to the event, register at the event
* runtime, in that they tell the runtime environment which
* method is to be executed when the event is raised. In this way
* many components can register for an event.
*
*---------------------------------------------------------------
REPORT  Z_006_EVENT.
*----------------------------------------------------------------
*       CLASS CL_NAME
*----------------------------------------------------------------
CLASS CL_NAME DEFINITION.
  PUBLIC SECTION.
    " DEFINE EVENT
    EVENTS OBJECT_CREATED
              EXPORTING VALUE(EX_OBJ) TYPE REF TO CL_NAME.
    METHODS: CONSTRUCTOR,
             " DEFINE EVENT HANDLER METHOD
             PROCESS_EVENT FOR EVENT OBJECT_CREATED OF CL_NAME.
  PRIVATE SECTION.
    DATA MSG(16) TYPE C.
             " register method with runtime will be executed
              " when event OBJECT_CREATED fires.
ENDCLASS.                    "CL_NAME
*----------------------------------------------------------------
*       CLASS CL_NAME IMPLEMENTATION
*----------------------------------------------------------------
CLASS CL_NAME IMPLEMENTATION.
  METHOD CONSTRUCTOR.
    MSG = 'OBJECT CREATED'.
    " Register the event handlers for the corresponding/all
    " instance/s.
    SET HANDLER PROCESS_EVENT FOR ALL INSTANCES.
    " Raise event OBJECT_CREATED.
    RAISE EVENT OBJECT_CREATED EXPORTING EX_OBJ = ME.
                           "ME refers to current instance
  ENDMETHOD.                    "CL_NAME
  " EVENT HANDLER
  METHOD PROCESS_EVENT.
    WRITE: 'EVENT FIRED :', ME->MSG.
  ENDMETHOD.                    "PROCESS_EVENT
ENDCLASS.                    "CL_NAME IMPLEMENTATION
DATA INSTANCE TYPE REF TO CL_NAME.
START-OF-SELECTION.
  CREATE OBJECT INSTANCE.
  CLEAR INSTANCE.
Program Output : 006
EVENT FIRED : OBJECT CREATED

VISIBILITY


An important feature of object-orientation is the encapsulation of attributes and methods - ultimately of functionality - in classes. A class guarantees its user specific properties and specific behavior. The sum of these properties is called the class interface. The Visibility mechanism defines the class interface which is available to the users.
There are three commonly defined types of visibility in object-oriented technology:
Public
The relevant class component (attribute, method, event etc.) is visible to all classes.
Protected
The relevant class component (attribute, method, event etc.) is visible to the class itself and all inheritors. (We will return to the terms Inheritor and Inheritance later in this document.)
Private
The relevant class component (attribute, method, event etc.) is only visible to the class itself.

OBJECT IDENTITY AND REFERENCE SEMANTICS


With the help of the previous Blog, you have established that objects belonging to a class are not created by the simple definition of the class. Neither does the instruction DATA: instance ref to CL_NAME creates an object. This instruction only creates a Reference, which in its initial state has the logical value INITIAL. Only with the instruction CREATE OBJECTinstance is the memory area for a new object requested from the system. The reference instance then refers to the object which has just been created. (The command CLEAR{{ instance. }}at this point means that the object, to which the reference variable refers, cannot be referenced. Therefore it can no longer be addressed in this program run. A Garbage Collector running in the background ensures that the object is removed from memory.
This separates object-oriented implementation from classic implementation. With the classic DATA instruction, main memory is reserved (which might never be used) and is pre-allocated the initial state of the relevant variable. With the "object-oriented" instruction DATA-x-TYPE-REF-TO, only the intention to create an object is expressed. The only storage space occupied is for an object reference.
In addition, every object has its own identity. Two references, which refer to objects, are only identical if they refer to the same object. Similarity between the attribute values of these objects is not the deciding factor. To get more idea about this see the following example.

CONSTRUCTOR


Objects must be created at runtime (using CREATE OBJECT). With their creation they also get their own identity. However, there are no fixed attribute values linked to the identity. You are probably already wondering how objects get to their initial state. How do objects recognize their initial attribute values?
TheConstructor concept exists specifically to answer this question. The constructor is a method which runs automatically during the creation of an object. The constructor allows you to define IMPORTING-parameters.
In ABAP Objects you differentiate between instance-dependent and class-dependent constructors via the language elements
METHODS{{ }}and CLASS-METHODS to be used in the definition part and via their namesconstructor and CLASS_CONSTRUCTOR:
The class constructor is called by the first access to a class element (method, attribute, event, and object), the (instance) constructor by the creation of an object (CREATE OBJECT).

Friday, August 19, 2011

Using Methods in Class


Methods describe the behavior of objects within a class. With the help of methods, the system provides operations, services and functions. Via methods, a user can manipulate the objects in a class or also the class itself. As for attributes, there are instance-dependent as well as class-dependent (static) methods. ABAP Objects differentiate between instance-dependent and class-dependent methods via the ABAP keywords METHODS or CLASS-METHODS used in the definition part.
In order to carry out instance-dependent (or instance-dependent) methods, the calling program needs a specific instance of the class. That is, the calling program must have a defined reference variable that points to a specific instance. Class methods are not instance-dependent. They can be called at any time by a user. To see how the syntax calls the various method types, see the following example.

Abap Interview Questions

A set of Abap Interview Questions.

Creating BAPI

A step by step procedure to create custom Bapi and register in BOR.

Click the below link to view

Thursday, August 18, 2011

ATTRIBUTES in CLASS


Attributes can take on values within an object at runtime. The sum of all attributes and their values describes the state of an object.
Attributes can be defined as instance dependent as well as Class dependent. Class attributes (Class attributes are also called static attributes.) are not tied to a single instance, rather they "belong" to all instances of the Class. These attributes exist only once in main memory. Instance-dependent attributes exist once per instance and are tied to a single instance.
In ABAP Objects you differentiate between instance-dependent and class-dependent attributes by means of the ABAP keywords DATA or CLASS-DATA to be used in the definition part:  

Creating Web Service

A step by step procedure to create Web Service and test the same.

Click the below link to view.

Thursday, August 11, 2011

Interactive ALV using Class

Interactive ALV Report using Class by implementing Double click Event.The sample code is follows: