Showing posts with label CLASS&OBJ. Show all posts
Showing posts with label CLASS&OBJ. Show all posts

Wednesday, September 5, 2018

modify constructor in sub class


   SIMPLE EXAMPLE TO SHOW THAT  SUB CLASS CAN MODIFY CONSTRUCTOR OF SUPER CLASS.

In this example , i have taken simple 3 classes , and constructor , if i want to add functionality to constructor we could do it in child class.

Each class has an instance constructor called constructor. This is an exception to the rule that states that component names along a path in an inheritance tree must be unique. The instance constructors of the various classes in an inheritance tree, however, are fully independent of one another. 

Constructor of the super class has to be called by using CALL METHOD SUPER->CONSTRUCTOR statement inside the implementation in sub classes.

NOTE:We cannot constructor like method , it would give an error.(You cannot call the special method 'CONSTRUCTOR' directly) .



*&---------------------------------------------------------------------*
*& Report  ZSR_CONSTRUCTORINHERITANCE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZSR_CONSTRUCTORINHERITANCE.

CLASS GRAND_FATHER DEFINITION.
PUBLIC SECTION.
DATAV_VAL1 TYPE CHAR40.
METHODS CONSTRUCTOR.
ENDCLASS.

CLASS GRAND_FATHER IMPLEMENTATION.
METHOD CONSTRUCTOR.
V_VAL1 'HI I AM FROM GRAND FATHER CLASS'.
WRITE/5 V_VAL1.
ENDMETHOD.
ENDCLASS.


CLASS FATHER DEFINITION INHERITING FROM GRAND_FATHER.
PUBLIC SECTION.
METHODS  CONSTRUCTOR.
ENDCLASS.

CLASS FATHER IMPLEMENTATION.
METHOD CONSTRUCTOR .
CALL METHOD SUPER->CONSTRUCTOR.
V_VAL1 ' HI I AM PARENT CLASS'.
WRITE:/5 V_VAL1.
ENDMETHOD.
ENDCLASS.


CLASS CHILD DEFINITION INHERITING FROM FATHER.
PUBLIC SECTION.
DATA:V_VAL3 TYPE CHAR40.
METHODS CONSTRUCTOR.
ENDCLASS.

CLASS CHILD   IMPLEMENTATION.
METHOD CONSTRUCTOR.
CALL METHOD SUPER->CONSTRUCTOR.
*CALL METHOD SUPER->
V_VAL1 ' HI I AM FROM GRAND CHILD CLASS'.
SKIP 2.
V_VAL3 'HI I AM THE NEW CREATED FROM GRAND CHILD CLASS'.
WRITE/5 V_VAL1,V_VAL3 .
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
DATAOBJ1 TYPE REF TO CHILD.
CREATE OBJECT OBJ1.

-------------------------output:---------------------------------------- 

Sunday, September 2, 2018

Instantiating a Class within another Class's Implementation


Instantiating a Class within another Class's Implementation


One class can be instantiated within the implementation of another class. Here the program contains two different classes cls1 and cls2. In the implementation of cls2 we are creating object obj1 ref to cls1 and then calling method of class 1 inside the 2nd class implementation.



REPORT  zsr_test NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
  PUBLIC SECTION.
    DATA v_cls1 TYPE char40 VALUE 'Class one data'.
    METHODS m_cls1.
ENDCLASS.                    "cls1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls2 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 DEFINITION.
  PUBLIC SECTION.
    DATA v_cls2 TYPE char40 VALUE 'Class two data'.
    METHODS m_cls2.
ENDCLASS.                    "cls2 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.
  METHOD m_cls1.
    WRITE: / v_cls1.
  ENDMETHOD.                                              
ENDCLASS.                   

*----------------------------------------------------------------------*
*       CLASS cls2 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls2 IMPLEMENTATION.
  METHOD m_cls2.
    DATA obj1 TYPE REF TO cls1.
    CREATE OBJECT obj1.
    CALL METHOD obj1->m_cls1.
    WRITE: / v_cls2.
  ENDMETHOD.                                               
ENDCLASS.                   

DATA obj2 TYPE REF TO cls2.

START-OF-SELECTION.
  CREATE OBJECT: obj2.
  CALL METHOD obj2->m_cls2.



IF you go for debugging , then you can see that system first calls m_class2 method and it holds the value of v_cls2 inside m_cls2


Class Definition Deferred

Class Definition Deferred.

 It is concept in which delay the definition of a class .This variant of the statement CLASS is used to make the class class known, regardless of the location of the actual definition of the class in the program. It does not introduce a declaration part and must not be ended using ENDCLASS.

One example of using the addition DEFERRED PUBLIC would be a type group in which a reference type is declared with a reference to a global class, which itself contains components with references to this reference type. In this situation, the entire class cannot be loaded before the type group, since the types are not yet known. After the statement DEFERRED PUBLIC, however, the class name can be specified after REF TO without the class having been loaded previously.[source:SAP KEY DEFINITION].

*&---------------------------------------------------------------------*
*& Report  ZCLASS_DEFERRED
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ZCLASS_DEFERRED.
CLASS ABC DEFINITION DEFERRED.

***NOW GOING TO DEFINE CLASS XYZ AND MAKING OBJ THAT REFERNCES THE CLASS ABC.

CLASS XYZ DEFINITION.
PUBLIC SECTION.
DATA OBJ1 TYPE REF TO ABC.
ENDCLASS.

CLASS ABC DEFINITION.
PUBLIC SECTION.
 DATA V1 TYPE CHAR40 VALUE 'HI I AM FROM CLASS ABC DEFINITION'.
ENDCLASS.

*******************************************************************
START-OF-SELECTION.
DATAOBJ2 TYPE REF TO XYZ.
CREATE OBJECT OBJ2,OBJ2->OBJ1.
WRITE:OBJ2->OBJ1->V1.




 

Inheritance in OOABAP simple example

INHERITANCE :
One of the most important concepts in object oriented programming is that of inheritance(common in all oops programming language). Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.
  • An object of one class can acquire the properties of another class.
  • Derived class inherits the data and methods of a super class. However, they can overwrite methods and also add new methods.
 SYNTAX:---
CLASS <subclass> DEFINITION INHERITING FROM <superclass>.
 
 
 

*&---------------------------------------------------------------------*

*& Report  ZINHERITANCE

*&

*&------------------------------------------------------------------

REPORT  ZINHERITANCE.

PARAMETERSs_matnr type mara-matnr.

DATAwa_mara TYPE mara.

DATAWA_MAKT_TEMP TYPE MAKT.

CLASS SUPER_cLA DEFINITION.

PUBLIC SECTION.

METHODS   GET_MATERIAL

        IMPORTING IM_MATNR TYPE MARA-MATNR

        EXPORTING EX_MARA TYPE MARA.

ENDCLASS.
CLASS SUPER_CLA IMPLEMENTATION.

  METHOD GET_MATERIAL.

    SELECT SINGLE FROM MARA INTO EX_MARA WHERE MATNR IM_MATNR.

  ENDMETHOD.

ENDCLASS.


******* FOR INHERITANCE ALWAYS WRITE INHERITANCE FROM AND 
 BESIDE  METHODS WRITE 
REDIFINITION*****
CLASS CHILD_CLAS DEFINITION INHERITING FROM SUPER_CLA.

  PUBLIC SECTION.

DATA wa_makt type makt.

METHODS :  GET_MATERIAL REDEFINITION"INHERITANING USING REDEFINTION"



ENDCLASS.

********CHILD CLASS IMPLEMENTATION******************
CLASS CHILD_cLAS IMPLEMENTATION.

METHODGET_MATERIAL.

  CALL METHOD SUPER->GET_MATERIAL "CALL METHOD TO GET MATERIAL

      EXPORTING

        IM_MATNR S_MATNR

      IMPORTING

        EX_MARA EX_MARA.

SELECT SINGLE from makt INTO WA_MAKT WHERE MATNR WA_MARA-MATNR.ENDMETHOD.

ENDCLASS.

START-OF-SELECTION. 
DATALO_OBJ TYPE REF TO CHILD_CLAS.

CREATE OBJECT LO_OBJ.      "CREATE OBJECT OF CHILD CLASS"

CALL METHOD LO_OBJ->GET_MATERIAL

EXPORTING

  IM_MATNR S_MATNR

  IMPORTING

       EX_MARA WA_MARA.



  WA_MAKT_TEMP LO_OBJ->WA_MAKT.



WRITE:/' MATERIAL DETAILS    'WA_MARA-MATNR WA_MARA-MTARTWA_MARA-MEINS,
 WA_MARA-MATKL.



WRITE/'MATERIAL DETAILED DESCRIPTION',WA_MAKT_TEMP-MATNR WA_MAKT_TEMP-MAKTX.