An abstract class (MustInherit in VB.NET) provides some common behavior
for other derived classes and
may enforce that derived classes implement certain methods. You write an abstract class
with the intent
to inherit from it.
You make a class sealed (NotInheritable in VB.NET) if you don?t
want to allow anyone to inherit or derive
from your class. This may be because your class does something so special that you
don?t want the derived
class?s implementation to override and negate that behavior. An example of sealed class
is the immutable class
String in .NET.
As you can see, abstract is intended to promote inheritance, sealed is
to disallow inheritance.
They are quite opposite.
Hema (one of my students) asked me if it makes sense to mark a class both as abstract and sealed.
My
quick answer to her was ?No!?
Then she pointed me to three classes in .NET 2.0 which are marked sealed and abstract!
For example, the MSDN documentation shows the TestConfiguration class,
in the namespace
Microsoft.VisualStudio.QualityTools.UnitTestFramework,
as:
Visual
Basic
NotInheritable MustInherit Public Class TestConfiguration
C#
public sealed abstract class TestConfiguration
I said to myself, this is Beta 2 documentation and may be it is wrong. So, I wrote a quick test:
Type
theType =
typeof(Microsoft.VisualStudio.QualityTools.UnitTesting.Framework.TestConfiguration);
Console.WriteLine("Is
TestConfiguration abstract?: {0}", theType.IsAbstract);
Console.WriteLine("Is
TestConfiguration sealed?: {0}", theType.IsSealed);
And the output I got is
Is
TestConfiguration abstract?:
True
Is TestConfiguration sealed?:
True
I am at a loss!
Can anyone educate me on this?
(a) Does it make sense for a class to be both abstract and sealed?
(b) What is the intent, for instance, to make TestConfiguration both sealed and abstract?
(c) How did they do that ? C++, C# and VB.NET compilers do not allow that.