SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

Inheritance - Subclass can re-implement inherited methods from superclass

Inheritance - Subclass can re-implement inherited methods from superclass

Subclass can re-implement the inherited public and protected methods from superclass.

Class C1 contains method METH1(public) and METH2(protected), both of which are modified and re-implemented in its subclass C2.Objects are created out of both classes and the method METH1 for both objects are called.

Output of the program demonstrates different behaviour for method METH1 of class C1 and C2.

This demonstrates the theme.



REPORT zstp_oo_abap.

CLASS c1 DEFINITION.
  PUBLIC SECTION.
    METHODS : meth1.
  PROTECTED SECTION.
    METHODS meth2.
ENDCLASS.

CLASS c1 IMPLEMENTATION .
  METHOD : meth1.
    WRITE:/5 'I am meth1 in class C1'.
    CALL METHOD meth2.
  ENDMETHOD.

  METHOD : meth2.
    WRITE:/5 ' I am meth2 in class C1 '.
  ENDMETHOD.
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1.
  PUBLIC SECTION.
    METHODS : meth1 REDEFINITION .
  PROTECTED SECTION.
    METHODS : meth2 REDEFINITION.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
  METHOD meth1.
    WRITE:/5 'I am meth1 in class C2'.
    CALL METHOD meth2.
  ENDMETHOD.

  METHOD : meth2.
    WRITE:/5 ' I am meth2 in class C2 '.
  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.
  DATA : oref1 TYPE REF TO c1,
         oref2 TYPE REF TO c2.
  CREATE OBJECT :  oref1 , oref2.
  CALL METHOD : oref1->meth1 ,
                oref2->meth1.

Output -

image

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in