ARTICLE

MustInherit in VB.NET

Posted by Neel Beniwal Articles | Visual Basic Language August 17, 2009
A MustInherit keyword applied on a class makes a class abstract class. This article demonstrates how to create an abstract class and use it in VB.NET.
 
Reader Level:

A MustInherit keyword applied on a class makes a class abstract class. An abstract class cannot be instantiated directly. It must be used through its derived classes. The purpose of an abstract class is to enforce must implementations in the base classes.

In the following example, the Human class is an abstract class with three MustOverride members.

MustInherit Class Human

    Public MustOverride Sub BodyStructure()

    Public MustOverride Property Hands()

    Public MustOverride Function MonkeyActions() As Boolean

End Class

When a class is derived from an abstract or MustInherit class, the derived class must override all inherited MustOverride members.

In this below code, class Person is inherited from class Human and overrides all MustOverride members of Human class.

Class Person

    Inherits Human

    Protected handsCount As Int16

 

    Public Overrides Sub BodyStructure()

 

    End Sub

 

    Public Overrides Property Hands() As Int16

        Get

            Return Me.handsCount

        End Get

        Set(ByVal value As Int16)

            Me.handsCount = value

        End Set

    End Property

 

    Public Overrides Function MonkeyActions() As Boolean

        Return True

    End Function

End Class

Important Rules of MustInherit Classes

  1. You cannot inherit a public class from an abstract class. If you do so, you will see the error shown in Figure 1.

    MI1.gif
  2. If you do not implement MustOverride members of an abstract class in a derived class, you will get errors as shown in Figure 2.

    MI2.gif
  3. All MustOverride members of a MustInherit class must be declared as Public.
  4. The members of a class shown in Figure can be used with MustOverride.

    MI3.gif


 

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor