Why jquery custom exception class should be final

I worked with IBM Rational Software Architect for Websphere Software (RAD) and ran the code analyzer. I received a warning that the custom Java exception class should be final. But there is no proper reason for this. In any case, for some requirements, we must extend custom exception classes to support the exception hierarchy. Thus, the final version is useless. In some cases, if we want to limit the creation of an object, we can mark contructor as private.

So, can any body please let me know why the custom exception class should be final?

+4
source share
4 answers

I would like to point you to the book Effective Java: Josh Bloch speaks with good arguments to mark the final classes or document extension points in order to maintain immutability and protect against unexpected behavior.

I mean Clause 17 (Design and document for inheritance or prohibition)

In any case, this may just be a recommendation, and if you are sure you can ignore it, just make sure you think about it.

+4
source

your own exception class will extend the exception class that I believe in. This custom class will be specific to the exception you want to handle. Therefore, if you really do not want to add another level of hierarchy to it, it makes sense to make this class final. If you want to create another special exception class, you can do this by extending the exception class again. You also get a warning that it should be final, but if you have any requirement that requires a higher level of inheritance hierarchy, you can determine that your class is not final.

0
source

The only reason to make the Final class is to not extend it with any other class. With that in mind, it's up to you whether you want to make the class final or not.

0
source

A good alternative to creating a class β€œfinale” would be to make its constructors private and instead provide static factories (also described in Josh Bloch's book). But of course, as already mentioned, it depends on the purpose of your class.

0
source

Source: https://habr.com/ru/post/1481705/


All Articles