Monday, February 7, 2011

Abstract Classes and Methods in Object Oriented Programming

Abstract Class: Classes which contain one or more abstract methods or abstract properties, such methods or properties do not provide implementation. These abstract methods or properties are implemented in the derived classes (Sub-classes).

Abstract classes does not create any instances to that class objects

Use of Abstract class:

We can define some common functionalities in Abstract class (Super-class) and those can be used in derived classes (Sub classes).

Step-by-Step Approach to create Abstract classes and Methods

TCode: SE24

Enter the name of class as 'Z_DEMO_ABS_CLASS' and press Create Button

A pop-up window is displayed, then select "Class" radio button and

Press enter

It will go to the next screen.

Here you enter the Description of the class and then select "Abstract" from Instantiation Drop down list to define the class an abstract class,

Then click on save button

Go to the "Attributes" tab,

Enter the Attribute name, Level, Visibility, Type and Description as shown in the screen shot.

Go to Methods tab,

Enter Method name, Level, Visibility and Description as shown in the below screen shot

Double click on the Method name "AREA"; it goes to method Implementation screen.

Go to Menu path, then Goto -> Method definition

Pop-up window is displayed. It gives complete information of the method



To define method "AREA" as an abstract method,

Go to "Attributes" tab, check the check box "Abstract"

When you click on the "Abstract" check box, pop-up window is displayed,

then press button.

Then press "Change" button.

A successful message is displayed like "Method changed successfully"

Creating Sub Class:

TCode: SE24

Enter the name of class as 'Z_DEMO_ABS_SUB_CLASS' and press Create Button to create sub class

A pop-up window is displayed, then select "Class" radio button and

Press enter

It goes to next screen, here you enter the Description of the class and then

Select the inheritance button , to inherit the super class then press button

Enter the Super class name as "Z_DEMO_ABS_CLASS", which is being created earlier and press button.

The Attributes and methods defined in the super class will automatically come into the sub class. See the below screen shots.

Go to the Methods tab, select the "AREA" method and click on

"Redefine" button

If you are not Redefine the method and trying to activate the class, it gives syntax error.



Here you can write the code

Write the code In between Method and End Method

                Method AREA
....
Endmethod

Write the below code

method AREA

* Local Data Declarations
DATA: lv_count TYPE i,
lv_res TYPE i.

* initialize Count value to '1'
lv_count = '1'.

DO 10 TIMES.
IF lv_count <= '10'.
lv_res = v_num * lv_count.
* Displa the multiplication table for a Given Number
WRITE: / v_num,
'*',
lv_count,
'=',
lv_res.
* Increment Count value
lv_count = lv_count + 1.
ELSE.
EXIT.
ENDIF.
ENDDO.
* Clear variable
CLEAR: v_num.

endmethod

Then save and activate the class and method.

Finally execute the class (F8)

It goes to below screen

Enter the number under V_NUM as "6" and press execute button .

The out will be displayed like below.




No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites