Is "sealed" necessary and when?

Joined
Apr 19, 2020
Messages
7
Programming Experience
1-3
Hi, everybody.
This operator is used so that it cannot be inherited from the class.
But in the example of Unity, they had the Application class "sealed", and then in the new versions they just made the class Application.

As I understand it, they did so that they could extend the Application class.

Question: do I need to use "sealed" at all and in what cases? If the class extension can be useful later at any time. For example, I now have a SteamManager class (for working with the SteamAPI), should I do it "sealed" ?

There is a feeling that they put "sealed", and then when the time comes they remove it editing the class and violating SOLID.
 
The sealed keyword is never strictly required and especially not in your own applications. You would generally declare a class sealed if it was defined in a library and you didn't want other users of that library to inherit that class. That might be the case if all members of the class were supposed to operate on data within your library. You'd need to consider it on a case by case basis but lots of classes are intended to be inherited. In this particular case, I'm not sure of their reasoning in either case but maybe they found that they thought sealing the class was a good idea but then lots of people had to implement complex workarounds to achieve what could be done easily by inheriting that class.
 

Latest posts

Back
Top Bottom