Friday, March 31, 2017

Guess the output : OO ABAP Friends Concept Sample Code



Friends Concept Example


*&---------------------------------------------------------------------*
*& Report YTEST_OOABAP3
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT YTEST_OOABAP3.

CLASS Test_Employee DEFINITION DEFERRED.

*----------------------------------------------------------------------*
* CLASS test_organization DEFINITION
*----------------------------------------------------------------------*
CLASS test_organization DEFINITION FRIENDS Test_Employee.

PUBLIC SECTION.

DATA : stock_price TYPE char10 VALUE 100.

METHODS display_stock_price.

PRIVATE SECTION.

DATA : avg_ctc TYPE char10 VALUE 200.

METHODS display_avg_ctc.

ENDCLASS. "test_organization DEFINITION

*----------------------------------------------------------------------*
* CLASS test_organization IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS test_organization IMPLEMENTATION.

METHOD display_stock_price.

WRITE / stock_price.

SKIP.

ENDMETHOD. "display_stock_price

METHOD display_avg_ctc.

WRITE / avg_ctc.

SKIP.

ENDMETHOD. "display_avg_ctc

ENDCLASS. "test_organization IMPLEMENTATION


*----------------------------------------------------------------------*
* CLASS Test_Employee DEFINITION
*----------------------------------------------------------------------*
CLASS Test_Employee DEFINITION INHERITING FROM test_organization.

PUBLIC SECTION.

METHODS : display_emp.

ENDCLASS. "Test_Employee DEFINITION


*----------------------------------------------------------------------*
* CLASS Test_Employee IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS Test_Employee IMPLEMENTATION.

METHOD display_emp.

DATA ref_empl TYPE REF TO test_organization.

CREATE OBJECT ref_empl.

CALL METHOD ref_empl->display_stock_price.
CALL METHOD ref_empl->display_avg_ctc.

ENDMETHOD. "display_emp

ENDCLASS. "Test_Employee IMPLEMENTATION

*----------------------------------------------------------------------*
* CLASStest_Shareholder DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS test_Shareholder DEFINITION INHERITING FROM test_organization.

PUBLIC SECTION.

METHODS display_shr.

ENDCLASS. "abc_shareholder DEFINITION

*----------------------------------------------------------------------*
* CLASStest_Shareholder IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS test_Shareholder IMPLEMENTATION.

METHOD display_shr.

DATA ref_shr TYPE REF TO test_organization.

CREATE OBJECT ref_shr.

CALL METHOD ref_shr->display_stock_price.
* CALL METHOD ref_shr->display_avg_ctc. " private method syntax error.

ENDMETHOD. "display_emp

ENDCLASS. "abc_shareholder IMPLEMENTATION

START-OF-SELECTION.

DATA : ref_emp TYPE REF TO Test_Employee,
ref_shr TYPE REF TO test_Shareholder.

CREATE OBJECT ref_emp.
CREATE OBJECT ref_shr.

CALL METHOD ref_emp->display_emp.

CALL METHOD ref_shr->display_shr.

OUTPUT
Test Program
      100
      200

      100

No comments:

Post a Comment