1. Tech

Your suggestion is on its way!

An email with a link to:


was emailed to:

Thanks for sharing About.com with others!

MustInherit and NotInheritable in VB.NET
What these new class modifiers mean in VB.NET
 Related Resources
Need a "getting started" guide for VB.NET?

Upgrading to VB.NET

Get VB.NET Cheap!


Rather than being just "Object Oriented," VB.NET is totally object driven. In VB.NET, everything is an object. Even variables. One way to get the total impact of this is to simply glance at the complete, expanded class view of a standard VB.NET form. There are thirteen levels of inheritance until you get to the great great great great great great great great great great great great great grandaddy of all VB.NET objects which is, appropriately enough, "Object". Talk about a big family!

VB.NET Class Hierarchy From Form

Entire books (lots of them, actually) are written about how to handle objects in VB.NET and we're not going to cover the entire subject in one article. But it's interesting to know that both MustInherit and NotInheritable are supported in VB.NET. In other words, you can create an object that can't be directly instantiated (you can't make a copy of the class directly in your code - more about this in a second) and you can create an object that can only be instantiated.

If any of you are Java fans, these two keywords roughly correspond to the "Abstract" and "Final" keywords. This explains why the classes that use these keywords are sometimes called Abstract and Final classes in VB.NET.

You would usually use the MustInherit modifier when you want all classes of a certain type to have some features, but you know the class will always have to be "completed" by a subroutine. VB.NET programmer Peter G. Aitken uses the example of geometric shapes to explain why you would want to use an Abstract class in his excellent article, Using Abstract Classes in Visual Basic.NET. See the Subjects menu on this page for more article links.

The NotInheritable modifier is not used as often and the concept of where and why you would use it isn't quite as clear.

First, a fundamental part of the .NET Foundation is to require that the purpose of every class is explicitly defined to remove all uncertainty and the eliminate a problem in previous OOP languages called “the fragile base class.” This happens when a base class adds a new method with the same name as a method name in a subclass that inherits from a base class. The programmer writing the subclass didn't plan on overriding the base class, but this is exactly what happens and it can create subtle bugs. (This is one cause of the cry of the wounded programmer, "I didn't change anything, but my program crashed anyway.") If there is a possibility that a class will be updated in the future and create this problem, declare it as NotInheritable.

You can see what happens when you try to inherit a NotInheritable class by writing some code to inherit the .NET Foundation system class Environment. This class happens to be marked NotInheritable in .NET Foundation. The VB.NET IDE simply rejects the statement.

Trying to Inherit from Environment

Next page > A Working Example of Class Inheritance

©2014 About.com. All rights reserved.