Gyan Factory

Gyan Factory
SAP Technical Project Support

Sunday, February 14, 2016

Child Class gets Access Public & Protected data-methods from Parent Class

Inheritance is one of a concepts of Object Oriented programming where we have a parent class and a child class. Here thechild class has the access of all of its parent class attributes and methods declared in Public and Protected sections only

We have created a program where we define a Parent class. Under public section we declare an attribute and a method. Similarly we have created attribute & method in Protected section also. Now in the implementation we set some function for both of the methods. Then we have created the child class Inheriting from parent class. We declare attribute & method in public section of the child class. Then we implement the class with having the functionality of the child class method. In the child class method we call the methods of parent class. After that in the start-of-selection we create an object for the child class. Here we are not creating object for parent class. Then we call the method of child class and we get all the data declared & implemented by parent class. Hence the child class has the access of all declared in public and protected section of Parent class.


*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION.
  PUBLIC SECTION.
    DATA v_par_pub TYPE char40.
    METHODS m_par_pub.

  PROTECTED SECTION.
    DATA v_par_pro TYPE char40.
    METHODS m_par_pro.
ENDCLASS.                    "parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
  METHOD m_par_pub.
    v_par_pub = 'This is Parent Class - Public Attribute'.
    WRITE / v_par_pub.
    SKIP.
  ENDMETHOD.                    "m_par_pub

  METHOD m_par_pro.
    v_par_pro = 'This is Parent Class - Protected Attribute'.
    WRITE / v_par_pro.
    SKIP.
  ENDMETHOD.                    "m_par_pro
ENDCLASS.                    "parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
  PUBLIC SECTION.
    DATA v_chi_pub TYPE char40.
    METHODS m_chi_pub.
ENDCLASS.                    "child DEFINITION

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
  METHOD m_chi_pub.
    v_chi_pub = 'This is Child Class - Public Attribute'.

    CALL METHOD: m_par_pub, m_par_pro.
    WRITE / v_chi_pub.
    SKIP.
    WRITE / '-----------End of Inheritance------------'.
  ENDMETHOD.                    "m_chi_pub
ENDCLASS.                    "child IMPLEMENTATION

START-OF-SELECTION.
  DATA obj_chi TYPE REF TO child.
  CREATE OBJECT obj_chi.
  CALL METHOD obj_chi->m_chi_pub.



Below is the output:


No comments:

Post a Comment