sealed and abstract: I?m at a loss? - No Fluff Just Stuff

sealed and abstract: I?m at a loss?

Posted by: Venkat Subramaniam on July 26, 2005

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.

Venkat Subramaniam

About Venkat Subramaniam

Dr. Venkat Subramaniam is an award-winning author, founder of Agile Developer, Inc., creator of agilelearner.com, and an instructional professor at the University of Houston.

He has trained and mentored thousands of software developers in the US, Canada, Europe, and Asia, and is a regularly-invited speaker at several international conferences. Venkat helps his clients effectively apply and succeed with sustainable agile practices on their software projects.

Venkat is a (co)author of multiple technical books, including the 2007 Jolt Productivity award winning book Practices of an Agile Developer. You can find a list of his books at agiledeveloper.com. You can reach him by email at venkats@agiledeveloper.com or on twitter at @venkat_s.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »