Monday, February 7, 2011

Final Classes and Methods in Object Oriented Programming

Final Class: A class that is defined as final class can not be inherited further. All Methods of a final class are inherently final and must not be declared as final in the class definition. Also, a final method can not be redefined further.
If only a method of a class is final then that class can be inherited but that method cannot be redefined.

Use of final class:
If you don't want anyone else to change or override the functionality of your class then you can define it as final. Thus no one can inherit and modify the features of this class.

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

Final Class:

TCode: SE24

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

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

Press enter

Enter the Description of the class and select the check box "Final" to define the class as Final class, and then press enter

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 "METH"; it goes to method Implementation screen.

Here write the code.

Write the following code in method meth

Method meth

* Local Data Declarations
DATA: lv_sum TYPE i VALUE '1',
lv_val1 TYPE i VALUE '0',
lv_val2 TYPE i VALUE '0'.

WRITE: / 'Fibonacci Series'.

WHILE lv_sum <= number.
WRITE: lv_sum.
lv_val1 = lv_val2.
lv_val2 = lv_sum.
lv_sum = lv_val1 + lv_val2.
ENDWHILE.

Endmethod.

Then save and activate the class and method.

Finally execute the class by pressing (F8) button

It goes to below screen, then enter value under "NUMBER" as "19" and

Press execute button .

The output will be displayed like below.

Final Method:

a) Creating Super Class:

TCode: SE24

Enter the name of class as 'Z_DEMO_SUP_CLASS' to create super class and then press "Create" Button

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

Press enter

Enter the Description of the class and then press enter

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 "VOLUM"; it goes to method Implementation screen. As shown below

To define method "VOLUM" as a Final method,

Go to Menu path, then Goto -> Method definition

Pop-up window is displayed

Go to "Attributes" tab, check the check box "Final" and then press "Change" button.

A successful message is displayed like "Method changed successfully"

Write the below code in Method volum

METHOD volum.
* Local Data Declarations
DATA: lv_vol TYPE i.

lv_vol = length * width * height.
WRITE: 'Volume of a Rectangle:', lv_vol.
* Clear variable
CLEAR: lv_vol.
ENDMETHOD.

Then save and activate the class and method.

b) Creating Sub Class:

TCode: SE24

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

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

Press enter

Enter the Description of the class and then select the inheritance button , to inherit the super class.

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

The Attributes and methods defined in the super class will automatically come into the sub class.

If you try to redefine or modify the super class method "VOLUM", go to the Methods tab, select the "VOLUM" method and click on

"Redefine" button ,

It gives a message like below and not allowed to redefine or modify the method in sub class.

The method implementation "VOLUM" can be used in both super class "Z_DEMO_SUP_CLASS" and sub class "Z_DEMO_SUB_CLASS".

Execute the sub class "Z_DEMO_SUB_CLASS" by pressing (F8) button

It goes to below screen, then enter values under "LENGTH, HEIGHT and WIDTH" as "2, 3 and 4" and Press execute button .

The output will be displayed like below.

Same as sub class, you can also execute the super class "Z_DEMO_SUP_CLASS", the same output will be displayed.

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