Final Classes and Methods in Object Oriented Programming

By Dharani Prasanna, IBM India

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.

Click here to continue...

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image