abstract class inheritance in visual basic.net

In this article, we will discuss how to implement inheritance using an abstract class. An abstract class is created as a base class and is inherited by other classes. Abstract classes are similar to interfaces(click here for explanation of what is interfaces). Abstract classes specify members that must be implemented in the inheriting classes. Note that, any class can inherit only from one abstract class. Also, we cannot create objects of an abstract class.

Creating an abstract class in visual basic 2008

In Visual Basic 2008, we create an abstract class by using the 'MustInherit' keyword. Like any other class, an abstract class can implement any number of methods.

Public MustInherit Class Employee
End Class
Creating a abstract members in abstract class

Abstract members of an abstract class do not contain any implementation code. The natural question to ask is which members have to be written as abstract members. Abstract members are members which have special implementation in derived classes and have no relevance in keeping them in abstract classes.

To declare an abstract member, we use the 'MustOverride' keyword. Abstract members are only declarable in abstract classes. The following code demonstrates the declaration and implementation of an abstract class - 'Employee'. The abstract method InComeEarned() is declared with MustOverride keyword and is overridden by subclasses.

Public MustInherit Class Employee
    Private mFirstName As String         'Employee first name
    Private mLastName As String          'Employee last name
    Public Sub New(ByVal fname, ByVal lname)
        mFirstName = fname
        mLastName = lname
    End Sub
    Public Property FName() As String
        Get                                  
            Return mFirstName
        End Get
        Set(ByVal value As String)              
            mFirstName = value
        End Set
    End Property
    Public Property LName() As String
        Get
            Return mLastName
        End Get
        Set(ByVal value As String)
            mLastName = value
        End Set
    End Property
    Public MustOverride Function InComeEarned() As Double
End Class
Implementing Abstract Class
Public Class RegularEmployee
    Inherits Employee

End Class
When a class is inherited from an abstract class, it must implement every abstract member defined by the abstract class. Refer the code given above. The method InComeEarned() is declared as an abstract member in abstract class - 'Employee', we have implemented this method using the Overrides keyword as shown below.

Public Class RegularEmployee
    Inherits Employee
    Private mSalary As Double
    Public Sub New(ByVal fname, ByVal lname, ByVal salary)
        MyBase.New(fname, lname)
        mSalary = salary
    End Sub
    Public Property Salary() As String
        Get                                   
            Return mSalary
        End Get
        Set(ByVal value As String)          
            mSalary = value
        End Set
    End Property
    Public Overrides Function InComeEarned() As Double
        Return Salary()
    End Function
End Class