0% found this document useful (0 votes)
130 views

06.inheritance in SAP Classes

The document discusses inheritance in SAP classes, including that inheritance allows a derived class to inherit properties from a super class and add new functionality. It provides an example of creating a super class and inheriting from it to create a child class that overrides a method to add additional material description details.

Uploaded by

Shashank Yerra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views

06.inheritance in SAP Classes

The document discusses inheritance in SAP classes, including that inheritance allows a derived class to inherit properties from a super class and add new functionality. It provides an example of creating a super class and inheriting from it to create a child class that overrides a method to add additional material description details.

Uploaded by

Shashank Yerra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

# Content

1 Inheritance Concept in SAP Classes

2 Using inheritance in SAP classes example

Inheritance Concept in SAP


Classes
Last Updated: November 12th 2013 by Ashok Kumar Reddy

What is inheritance in SAP Classes, uses of inheritance, properties of inheritance


in SAP OOABAP

+ -

Inheritance
Inheritance is the concept of passing the behavior of one class to another class.

You can use an existing class to derive a new class.

Derived class inherits the data and methods of a super class.

However they can overwrite the existing methods also add new code. Advantage of this property is
re-usability.

This means we can add additional features to an existing class without modifying it.

SUPER is the keyword used to represent the super class in oops, you can access the methods and
attributes the super class using this word super.

REDIFINATION is the keyword which is used to overwrite the parent class methods with new definition.

NOTE: In Object Oriented ABAP we have only single inheritance. There is no multiple inheritance.

Inheritance in general
Super Class Is a main class by using this we can derive a new class which will be called as Child
class.
Final Class is a class which can not be used for inheritance, it is a class property (check box under
properties of class).

If the final check box is selected, we can not use this class for inheritance, to use inheritance remove
the final check box.

Learner Questions

No Questions by learners, be first one to ask ..!!

Using inheritance in SAP classes


example
Last Updated: November 13th 2013 by Ashok Kumar Reddy

Working with inheritance in SAP classes, using inheritance in SAP classes

+ -
Go to SE24, provide name as ZCL_SAPN_SUPERCLASS, create
A pop up will open provide description, un-select final check box, save and save as local object.

Go to methods tab, provide method name as GET_MATERIAL_DETAILS-INSTANCE-PUBLIC.


Go to parameters tab and add below paramaters.

Go back to methods, double click on method name and add below code.

METHOD GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.

ENDMETHOD.

Save and activate the class.


Super class is created, now we have to inherit this class and derive a new class(child class).

Go to SE24, provide name as ZCL_SAPN_CHILDCLASS, create.

A pop up will open provide description, click on create inheritance icon(see below image), provide
super class name.
Save, save as a local object, now you can see the inherited method, using inheritance we can
overwrite the method implementation to do this click on redifinition icon (see image below)
We will add additional functionality to get material description details along with material details, go
to attributes tab and add a attribute ls_makt-instance-public-makt.

Go back to methods, doublle click on method, uncomment the inherited code, pass parameters and
add additional code to get material descriptions.
METHOD GET_MATERIAL_DETAILS.

CALL METHOD SUPER->GET_MATERIAL_DETAILS


EXPORTING
IM_MATNR = IM_MATNR
IMPORTING
EX_MARA = EX_MARA.
***Additional code to get material descriptions
SELECT * FROM MAKT
INTO LS_MAKT
WHERE MATNR = EX_MARA-MATNR.
ENDSELECT.

ENDMETHOD.

Using Class inprogram


Create a program in SE38 and follow below steps(code).
REPORT ZSAPN_CLASS_INHERITANCE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CHILDCLASS. "declare class
DATA : WA_MARA TYPE MARA. "declare MARA
DATA WA_MAKT TYPE MAKT. "declare MAKT

PARAMETERS P_MATNR TYPE MARA-MATNR. "material input


CREATE OBJECT LO_CLASS. "create instance for class

START-OF-SELECTION.
CALL METHOD LO_CLASS->GET_MATERIAL_DETAILS "get material details
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.

WA_MAKT = LO_CLASS->LS_MAKT. "access material description from class attribute


WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL. "print material
details
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX. "print material descriptions

Learner Questions

No Questions by learners, be first one to ask ..!!

You might also like