SAP Tech Pro

SAP Technical Blog
Topprice Price Comparison

Method of a parent class, used from its subclass, uses attributes of the parent class only, if the method is not re-defined in subclass.

Method of a parent class, used from its subclass, uses attributes of the parent class only, if the method is not re-defined in subclass.

As long as a method (using private attributes) inherited from a superclass is not redefined, it still uses the private attributes of the superclass, not those of the subclass, even if the subclass has private attributes of the same name.

 

Class C1 contains a method M1 in the public section and a private variable, NUM of value = 5. Method M1 in class C1 displays the value of private variable, NUM.

Class C2 is a subclass of class C1. It does not redefine method M1. But, it has also a private variable , NUM with value = 6.

An object is created from class C2 and the method M1 is called.

The output shows that the variable NUM( as displayed by method M1) has been taken from class C1, not C2.

This establishes the theme.



REPORT zstp_oo_abap.

CLASS c1 DEFINITION.
  PUBLIC SECTION.
    METHODS : m1.
  PRIVATE SECTION.
    DATA : num TYPE i VALUE 5.
ENDCLASS.

CLASS c1 IMPLEMENTATION.
  METHOD : m1.
    WRITE:/5 num.
  ENDMETHOD.
ENDCLASS.

CLASS c2 DEFINITION INHERITING FROM c1.
  PUBLIC SECTION.
    DATA : num TYPE i VALUE 6.
ENDCLASS.

CLASS c2 IMPLEMENTATION.
ENDCLASS.


START-OF-SELECTION.
  DATA :  oref2 TYPE REF TO c2.

  CREATE OBJECT : oref2.

  CALL METHOD oref2->m1.

Output -

Object Oriented Programming - Method Subclasses

Add comment

Before Buying, Compare Prices at Topprice.in

Topprice.in