Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Why Final Classes are used in ABAP

Former Member
0 Kudos

Hi Everyone,

    Can you tell me why we declare a class as Final in ABAP? I have given the answer that -A class is declared Final if I don't want that class to be extended(Inherited). But this answer doesn't satisfy the interviewer. He insisted to know -What is the purpose for not extending the class? Why we need to declare a class as Final. So please can someone give me more satisfying or real-time example of " Why we declare a class as Final In ABAP" ?

Thanks in advance-

Amitanshu

1 ACCEPTED SOLUTION

nomssi
Active Contributor

Hello Amitanshu,

inheritance is both powerful and dangerous [9]. We should use FINAL to prevent abuse.

Because of its power, we tend to use inheritance as a powerful macro (so called implementation inheritance) just for DRY. But if the subclass is not a problem domain specialization (a subtype) of the super class (LSP [1] / [2]), this will lead to non intuitive errors, like a circle is not an ellipse ([3], [4], [5]).

SAP recommend ([6], [7] : -broken links- defensive programming) to declare your classes FINAL by default. If you are really defensive, you will be using composition over inheritance more often.

The circle/ellipse example: instead of creating a subclass of ellipse, you subclass both circle and ellipse from an abstract geometric object class. To force this decision in future code maintenance, the implementation of both circle and ellipse classes is set to FINAL.

In this case, FINAL is used to indicate a design choice: subclass the geometric object abstract class instead.

regards,

JNN

[6] ABAP Programming Guidelines by Horst Keller

[9] Testing Object-Oriented Systems - Models, Patterns, and Tools, by from Binder, R.

chap. 4.2.3

Inheritance weakens encapsulation, creating a bug hazard similar to global data [..] Deep and wide inheritance hierarchies can defy comprehension, leading to bugs and reducing testability.[..]

Inheritance can be used to implement specialization relationships or as a programming convenience as an unusually powerful macro substitution.

4 REPLIES 4

gouravkumar64
Active Contributor
0 Kudos

Hi,

See u r partially right.

If u do not want that,anyone change or override the functionality of that class,u can declare it as final.

as u r telling it can not be inherited further also,it is also right.

A final class can not be redefined further.

Lastly if only a method of class is final,then that class can be inherited but that method can not be re defined.

there r many example of FINAL CLASS IN websites.

please search for example.

Hope I am little bit clear to u.

Thanks

Gourav.

Former Member
0 Kudos

Hi,

    creating a class as a final means you can stop polymorphism. 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.

regards

Sabyasachi

Former Member
0 Kudos

Hi Amitanshu,

Hmmm.. Good question. Given that you already know the properties of a final class, I need not explain  again on its properties.

However, Many a times  , we study (including myself!) OO concepts without understanding on how they could be actually applied in the real world.

Ok, One thought about final classes that come to my mind is that, A Final class declred as abstract is not logical, as instance methods cannot be used.

Hence, a Final class is always to be instantiated.So , lets say you have an inheritance hierarchy, within the inheritance hierarchy, you would  typically want to ensure to not instatiate the Super classes, rather instantiate the concrete(final) subclasses only. In such a scenarion, you could have the  super classes defined as abstract and the concrete one's as final, thereby preventing instantiation of the super class because an abstract class cannot be instantiated.

A typical example : The inheritance hierarchy of a Vehicle.

Vehicle-->Car-->Sedan-->Fod-->Fod X1 ()etc. Now, the final class is the Fod X1, you dont want to instantiate a Car object or a Sedan object as it certainly is not logical, hence you typically define them as abstract and only the Fod X1 as final, which makes sense. Also you cannot inherit another variant from Fod X1 . In the above hierarchy, the only (tangible)object that can be logically instantiated is the Fod X1. .

You can also see this in the SAP environment, a typical example is the class CL_SALV_TABLE, which is final. If you notice , all the classes in the hierarchy above CL_SALV_TABLE are all abstract, CL_SALV_MODEL_LIST,CL_SALV_MODEL_BASE ...and so on. You would not want to iinstantiate an object of CL_SALV_MODEL_LIST , as it is just an abstraction and CL_SALV_TABLE is the final product of all the abstractions combined.

Do correct me if I am missing anything.

Thanks,

Venkat.

Message was edited by: Venkat Gowrishankar

nomssi
Active Contributor

Hello Amitanshu,

inheritance is both powerful and dangerous [9]. We should use FINAL to prevent abuse.

Because of its power, we tend to use inheritance as a powerful macro (so called implementation inheritance) just for DRY. But if the subclass is not a problem domain specialization (a subtype) of the super class (LSP [1] / [2]), this will lead to non intuitive errors, like a circle is not an ellipse ([3], [4], [5]).

SAP recommend ([6], [7] : -broken links- defensive programming) to declare your classes FINAL by default. If you are really defensive, you will be using composition over inheritance more often.

The circle/ellipse example: instead of creating a subclass of ellipse, you subclass both circle and ellipse from an abstract geometric object class. To force this decision in future code maintenance, the implementation of both circle and ellipse classes is set to FINAL.

In this case, FINAL is used to indicate a design choice: subclass the geometric object abstract class instead.

regards,

JNN

[6] ABAP Programming Guidelines by Horst Keller

[9] Testing Object-Oriented Systems - Models, Patterns, and Tools, by from Binder, R.

chap. 4.2.3

Inheritance weakens encapsulation, creating a bug hazard similar to global data [..] Deep and wide inheritance hierarchies can defy comprehension, leading to bugs and reducing testability.[..]

Inheritance can be used to implement specialization relationships or as a programming convenience as an unusually powerful macro substitution.