Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Purpose of final and sealed

Tags:

oop

final

sealed

Why would anyone want to mark a class as final or sealed?

like image 709
Chad Avatar asked Sep 20 '09 03:09

Chad


People also ask

What is the purpose of sealed?

The original purpose was to authenticate a document, or to prevent interference with a package or envelope by applying a seal which had to be broken to open the container (hence the modern English verb "to seal", which implies secure closing without an actual wax seal).

What is the point of sealed classes?

2. What Is a Sealed Class? Sealed Classes allow us to fix type hierarchies and forbid developers from creating new subclasses. They are useful when we have a very strict inheritance hierarchy, with a specific set of possible subclasses and no others.

Is Sealed same as final?

final : Cannot be extended further. sealed : Can only be extended by its permitted subclasses. non-sealed : Can be extended by unknown subclasses; a sealed class cannot prevent its permitted subclasses from doing this.

Why do we need final class in Java?

The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class. You cannot extend a final class.


2 Answers

According to Wikipedia, "Sealed classes are primarily used to prevent derivation. They add another level of strictness during compile-time, improve memory usage, and trigger certain optimizations that improve run-time efficiency."

Also, from Patrick Smacchia's blog:

  • Versioning: When a class is originally sealed, it can change to unsealed in the future without breaking compatibility. (…)

  • Performance: (…) if the JIT compiler sees a call to a virtual method using a sealed types, the JIT compiler can produce more efficient code by calling the method non-virtually.(…)

  • Security and Predictability: A class must protect its own state and not allow itself to ever become corrupted. When a class is unsealed, a derived class can access and manipulate the base class’s state if any data fields or methods that internally manipulate fields are accessible and not private.(…)

Those are all pretty good reasons - I actually wasn't aware of the performance benefit implications until I looked it up just now :)

The versioning and security points seem like a huge benefit in terms of code confidence, which is very well justified on any kind of large project. It's no drop-in for unit testing, of course, but it would help.

like image 106
Mark Rushakoff Avatar answered Sep 23 '22 15:09

Mark Rushakoff


Because creating a type for inheritance is much harder work than most folks think. It is best to mark all types this way by default as this will prevent others from inheriting from a type that was never intended to be extended.

Whether or not a type should be extended is a decision of the developer who created it, not the developer who comes along later and wants to extend it.

like image 31
Andrew Hare Avatar answered Sep 21 '22 15:09

Andrew Hare