Friday, December 18, 2015

OO ABAP: Casting

Use of Casting

Customer specific changes can be easily implemented using casting. Suppose there are two customers, customer1 and customer2. They have some common properties and some individual customer specific properties. So we can maintain their common properties in class general(parent) and individual properties in class customer1(child) and customer2(child). Casting help as us to access these child properties using parent. ie, we don't need to make changes in our programs whenever the individuals customer properties changes. 


Narrow Casting

The assignment of subclass instance to super class instance is narrow casting, because we are switching from 'more detailed view' to 'less detailed view'.

super class = sub class

Widen Casting

The assignment of super class instance to subclass instance is widen casting, because we are switching from 'less detailed view' to 'more detailed view'.

sub class ?= super class


Up Casting and Down Casting

These terms are little confusing. It depends on the view perspective from where we are seeing the inheritance tree. Please see the diagrammatic explanation below.
Later version of SAP follows DOWN-TOP tree structure (Diagram 1).

Down Casting (Narrow Casting)


1. Create a class (Super/Root) with common_method


2. Create a sub class for customer 1 by inheriting super class


3. Redefine COMMON_METHOD in sub class and also create a new method 'METHOD_CUSTOMER1'


4. Call sub class methods using super class object


REPORT  zdil_blog_downcasting.

DATA: lo_general   TYPE REF TO zdil_blog_class_genral,
      lo_customer1 TYPE REF TO zdil_blog_class_customer1.

START-OF-SELECTION.

  CREATE OBJECT lo_general.
  CREATE OBJECT lo_customer1.

* Down Casting

  lo_general = lo_customer1.

  CALL METHOD lo_general->common_method( ).


5. Output


Though we called 'COMMON_METHOD' of super class, system called redefined 'COMMON_METHOD' of sub class due to Down Casting effect.

Up Casting (Widen Casting)


To access the newly created methods of sub class, we can use Up Casting. We have do Down Casting before Up Casting, otherwise it gives dump. 

Sample code


REPORT  zdil_blog_upcasting.
DATA: lo_general   TYPE REF TO zdil_blog_class_genral,
      lo_customer1 TYPE REF TO zdil_blog_class_customer1,
      lo_customer1_temp TYPE REF TO zdil_blog_class_customer1.

START-OF-SELECTION.

CREATE OBJECT lo_customer1_temp.

* Down Casting
  lo_general = lo_customer1_temp.

* Up Casting
  lo_customer1 ?= lo_general.

  CALL METHOD lo_customer1->method_customer1( ).

Output









Wednesday, December 16, 2015

OO ABAP: Event with Parameter

Event with Parameter

While raising an event, we can export parameter values. Later we can import these values in event-handler method and can be used for further data processing.

Please go through OO ABAP: Event  to see how to create simple event.

Following are the simple 5 steps for Raising & Handling event with parameter. 

1. Define an Event with parameter in a class 



2. Define a method with importing parameter (values will be imported from event)in same class or other class




3. Link event and method to convert the method into an event handler method. Click the above 'Detail view' button for linking.


4. Create a triggering method with parameter and raise the event exporting value for parameter.


5. Use SET HANDLER and register the event handler method for a particular instance in the program.


Sample Program 

REPORT  zdil_blog_event_para.

DATA: lo_class1 TYPE REF TO zdil_blog_class1,
      lo_class2 TYPE REF TO zdil_blog_class2.

START-OF-SELECTION.

  CREATE OBJECT lo_class1.
  CREATE OBJECT lo_class2.

  SET HANDLER lo_class2->event_handling_method FOR lo_class1.

  lo_class1->event_triggering_method( 'TEST_PARA' ).

Output




Monday, December 14, 2015

OO ABAP: Events

Events

Raising events in Object Oriented Programming is a mechanism by which methods of one class can call methods of same/other classes.

5 simple steps for Raising & Handling an event

1. Define an Event in a class


2. Define a method in same class or other class




3. Link event and method to convert the method into an event handler method. Click the above 'Detail view' button for linking.


4. Create a triggering method which will raise the event



5. Use SET HANDLER and register the event handler method for a particular instance in the program. ie, we inform the system that the method will be triggered by an event.


Sample Program 

REPORT  zdil_blog_event_demo.

DATA: lo_class1 TYPE REF TO zdil_blog_class1,
      lo_class2 TYPE REF TO zdil_blog_class2.

START-OF-SELECTION.

  CREATE OBJECT lo_class1.
  CREATE OBJECT lo_class2.

  SET HANDLER lo_class2->event_handling_method FOR lo_class1.

  lo_class1->event_triggering_method( ).

Output