Ingo says that "sealed" classes are a good thing and goes great lengths to explain why he thinks so in this article.

Now, I do have a little problem with his conclusion (which I still partially share, because I do write sealed classes every once in a while, but for a slightly different reason, see below) because the example he's using isn't really fitting the problem and he actually makes some assumptions about the ImageList class that aren't accurate in this context. 

First off, the property ImageList.Images isn't virtual and therefore cannot be overridden, at all. So that by itself is no reason to make the class sealed. The property has been introduced by the class, the class designer chose not to make it virtual. Can't be overriden, done. The same is true for all other properties except Container and Site, which are inherited from System.ComponentModel.Component. I fail to see anything on the ImageList class (not even a method) that really justifies the lockup using sealed considering the current version of the Framework. The bad that sealed does here is that I can't create a wrapper around the ImageList that simplifies setting up such a list in my specific environment.

Using sealed on a class is a very brutal thing to do. A more gentle way of using the sealed keyword is to say [void sealed override myMethod() { }], which essentially says: "Within my own class space I am using inheritance for this method, but since outsiders don't know what I am doing I won't let them override the behavior from here downwards in the class hierarchy." That doesn't affect the inheritability of the entire class and hence doesn't adversely affect the ability to wrap the existing functionality. 

Now ... is sealed on a class just plain evil? There are two answers:

  • Yes. The keyword sealed on a class usually doesn't have much or any of a technical justification in the "here and now" version of a class library.
  • No. The keyword sealed conveys a very clear message that the class designer reserves the right to change the class so dramatically in a future release of the product that you would be tremendously unhappy if you had derived from it to implement your own functionality, once that new version comes along. Sealed says: This is going to change in a big way.

 

Categories: CLR

Sunday, July 27, 2003 7:46:35 AM UTC
>> "First off, the property ImageList.Images isn't virtual and therefore cannot be overridden, at all. So that by itself is no reason to make the class sealed. The property has been introduced by the class, the class designer chose not to make it virtual. Can't be overriden, done." <<

There is just one aspect that should not be forgotten. Although it is not possible to override a method of your base class which is not marked virtual, you still have the possibility to “hide” the base class implementation by using the new operator [C#] on the method. The only way to prevent this is marking the class as “sealed”.

Applied to Ingo’s ImageList a class inherited form ImageList could provide an own implementation of the Images-property and that can lead to problems (as Ingo explained).
Sunday, July 27, 2003 12:45:25 PM UTC
It doesn't lead to any problems if you hide a member, because you need to do that explicitly using the "new" keyword and therefore you know and everybody else can find out via documentation or via Reflection that you introduced the property and not the base class whose methods you are hiding.

If you were to hide a base class member of -- say -- ImageList and you'd pass an instance of that new class to a method expecting an ImageList, that method would of course be seeing just an ImageList and nothing of the new behavior.
Sunday, July 27, 2003 5:13:23 PM UTC
You're right. It doesn't lead to problems in that scenario. I don’t even think that “sealed” was made to prevent errors. The scenario I thought of is that with using "sealed" I want to forbid the user to write his own controls that use his "new" implementation of the ImageList. So it’s more like a hint for inexperienced OO-programmers that doing that is not the best way. If I have no virtual members and it does not make sense to inherit from the class, than why should I allow him to do that? Sure, as an experienced programmer the “sealed” (especially in ImageList) hurts, but I think it was intended to make clear that the designer does not suggest you to inherit from the class and to prevent people writing “bad code” or hacks.

So sealed is -- at least -- not bad (or even evil); although it is questionable if the ImageList should hade been sealed by the Windows Forms team.
Saturday, July 10, 2004 4:46:18 AM UTC
I added the suggestion to unseal ImageList to Microsofts .NET feedback site. Please go check it out and vote for it.

http://lab.msdn.microsoft.com/ProductFeedback/viewfeedback.aspx?
feedbackid=4cd17007-6597-485e-bc7e-1cee21b07ebb

Thanks,
Robert
Comments are closed.