Gyan Factory

Gyan Factory
SAP Technical Project Support

Sunday, February 14, 2016

Abstract Class cannot be Instantiated

Abstract class cannot be instantiated. Only the sub classes defined from the abstract class can be instantiated. Here all the data and methods declared in public and protected section of the abstract class are accessed by all sub classes.

Here is a program where we have defined an Abstract class and implement that with a method. Then we have defined a child class inheriting from abstract class and implement that class with another method. In this child class method we call the abstract class method and also add some functionality. Now in start-of-selection we have created object for abstract class and child class. In this position when we compile the program a syntax error is coming because the Abstract class cannot be instantiated

So we comment that part and then compile it successfully. Here the child class have the access of data and methods from the abstract class since it has been inherited from the abstract class.


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

REPORT  zsr_test.

*----------------------------------------------------------------------*
*       CLASS parent DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent DEFINITION ABSTRACT.
  PUBLIC SECTION.
    DATA v_text TYPE char50.
    METHODS m_abs.
ENDCLASS.                    "parent DEFINITION

*----------------------------------------------------------------------*
*       CLASS parent IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS parent IMPLEMENTATION.
  METHOD m_abs.
    v_text = 'Method in Abstract Class Public section'.
    WRITE / v_text.
    SKIP.
  ENDMETHOD.                    "m_abs
ENDCLASS.                    "parent IMPLEMENTATION

*----------------------------------------------------------------------*
*       CLASS child DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child DEFINITION INHERITING FROM parent.
  PUBLIC SECTION.
    METHODS m_chi.
ENDCLASS.                    "child DEFINITION

*----------------------------------------------------------------------*
*       CLASS child IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS child IMPLEMENTATION.
  METHOD m_chi.
    CALL METHOD m_abs.
    v_text = 'Method in Sub Class Public section'.
    WRITE: / v_text,
           / 'Today is = ', sy-datum DD/MM/YYYY.
  ENDMETHOD.                    "m_chi
ENDCLASS.                    "child IMPLEMENTATION

START-OF-SELECTION.
  DATA: obj_par TYPE REF TO parent,
        obj_chi TYPE REF TO child.

  CREATE OBJECT: obj_chi.
  "obj_par. Abstract Class cannot be Instantiated
  CALL METHOD: obj_chi->m_chi.



Below is the Output:


No comments:

Post a Comment