banner ads

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 NO STANDARD PAGE HEADING.

*----------------------------------------------------------------------*
*       CLASS cl_parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent DEFINITION.
  PUBLIC SECTION.
    "Accessible to child class
    DATA v_parent_pub TYPE char40.
    METHODS m_parent_pub.

  PROTECTED SECTION.
    "Accessible to child class
    DATA v_parent_pro TYPE char40.
    METHODS m_parent_pro.

  PRIVATE SECTION.
    "Not Accessible to child class
    DATA v_parent_pri TYPE char40.
    METHODS m_parent_pri.
ENDCLASS.                    "cl_parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_parent IMPLEMENTATION.
  METHOD m_parent_pub.
    v_parent_pub = 'Parent Class Public data'.
  ENDMETHOD.                    "m_parent_pub

  METHOD m_parent_pro.
    v_parent_pro = 'Parent Class Protected data'.
  ENDMETHOD.                    "m_parent_pro

  METHOD m_parent_pri.
    v_parent_pri = 'Parent Class Private data'.
  ENDMETHOD.                    "m_parent_pri
ENDCLASS.                    "cl_parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS cl_child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child DEFINITION INHERITING FROM cl_parent.
  PUBLIC SECTION.
    DATA v_child_pub TYPE char40.
    METHODS m_child_pub.
ENDCLASS.                    "cl_child DEFINITION

*----------------------------------------------------------------------*
*       CLASS cl_child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cl_child IMPLEMENTATION.
  METHOD m_child_pub.
    v_child_pub = 'Child Class Public data'.

    "Child can access public & protected methods
    CALL METHOD: m_parent_pub,
                 m_parent_pro.

    "Child can access public & protected data
    WRITE: / v_parent_pub,
           / v_parent_pro,
           / v_child_pub.
  ENDMETHOD.                    "m_child_pub
ENDCLASS.                    "cl_child IMPLEMENTATION

START-OF-SELECTION.  DATA obj TYPE REF TO cl_child.
  CREATE OBJECT obj.
  CALL METHOD obj->m_child_pub.
 
Now we have some step by analysis in debugging mode. At first we set the break point at calling method and controller will enter into the method. Here we can see that the child class have access to all public & protected data of parent class. It doesn't have any access to it's private data.


So it populates all of those accordingly.


Below is the output:

No comments