1. Computing

MustInherit and NotInheritable in VB.NET

Controlling object inheritance

From , former About.com Guide

Updated May 31, 2009

Inheritance is the way Visual Basic .NET makes it possible for programming code to use earlier code that has already solved some problem. When code already exists that can create an "employee" object, why write it again? If other programmers have figured out how to calculate the interest payment on a loan, you ought to use their work. This is a lot more than simple "code reuse" ... it's also a way to make sure that "business rules" are followed and prevent errors. But to do all these things, your code has to be able to control inheritance. The MustInherit and NotInheritable modifiers are one of the ways to do that. Using these keywords, you can create an object that can't be directly instantiated (you can't make a copy of the class directly in your code) or create an object that can only be instantiated.

If any of you have used Java, these two keywords roughly correspond to the "Abstract" and "Final" keywords in that language. This is why you might see some Visual Basic documentation that refers to classes that use these keywords as Abstract and Final classes.

MustInherit

MustInherit is usually used when you want all classes to have some basic features, but you also know that the class will always have to be "completed" with more code before it's actually used in a program. In other words, a class modified with MustInherit can't be used by itself. The methods and properties in it can only be used after another class has inherited it. A class that uses the MustInherit keyword is usually called a "base class" in VB.NET.

There are examples of MustInherit classes in the .NET Framework itself. One of them is the WebRequest class. The Microsoft documentation states that the declaration of the class is:

 <Serializable>
 MustInherit Public Class WebRequest
    Inherits MarshalByRefObject
    Implements ISerializable 

This is a good example of how classes are declared in real-world, state-of-the-art programming. (We won't discuss the other keywords in this article, however.)

According to Microsoft:

"An application that uses the request/response model can request data from the Internet in a protocol-agnostic manner, in which the application works with instances of the WebRequest class while protocol-specific descendant classes carry out the details of the request."

The idea is that WebRequest can't do anything without knowing what web protocol is being used. So it can't be instantiated as an object by itself. It can only be inherited. This is what happens when you try to instantiate it:

--------
Click Here to display the illustration
Click the Back button on your browser to return
--------

In the specific case of WebRequest, Microsoft provides a method to create a new, inherited object called the Create method to save you the trouble of adding the inheritance code to your project because there are a lot of other required details to code if you inherit WebRequest in your own local class. But if you wanted to, you could start the code to implement it this way:

 Imports System.Net
 Public Class myWebRequest
     Inherits webrequest
 ...
 End Class 

... and then ...

 Dim myWebRequestInstance As New myWebRequest 

MustInherit Classes and Interfaces

If you're an experienced VB programmer, you might be reading this and wondering what the difference is between MustInherit and an Interface class. (Interface classes are explained in my article, Using Interfaces In Separate Files. If a MustInherit class only contains MustOverride (abstract) members, then it's about the same thing. But MustInherit classes can also contain regular members and an interface can't do that.

On the other hand, you can code a class that can implement more than one interface but you can only inherit from one class. (C++ allows "multiple inheritance" and it causes problems. Visual Basic doesn't.) But you can code a class that inherits from a MustInherit class and the subclass can implement more than one interface. So you can get the best of both worlds that way.

The NotInheritable modifier is used mainly to prevent a particular type of error. The next page tells you what it is.

  1. About.com
  2. Computing
  3. Visual Basic
  4. Using VB.NET
  5. MustInherit and NotInheritable in Visual Basic .NET

©2013 About.com. All rights reserved.