Abstract class constructors

Many programmers wonder about the use of having an abstract class constructor, in other words having a constructor in an abstract class. This doubt arises because we cannot create an instance of an abstract class, whereas for all other classes we can create an instance.

Ans:Derived classes do not inherit base class constructor. So, to initialize base class objects, we need to call the base class constructor in the derived class constructor.

Another question programmers ask frequently is when we are not able to create an instance of the abstract class, what is the purpose and use of writing code in abstract class?

Ans:We design an abstract class in such a way that it always serves as the base class for other classes and specify the functionality the derived classes should have. The functionality which is common to all inherited classes can be written here. Code reuse is the main purpose of creating an abstract class.

One additional question is how can we call the methods of the abstract class without an instance?

Ans: We can call base class methods using derived class object. Note that we cannot call derived class methods using base class object.

The detailed explanation is given below.

The code given below is for the base class 'Employee' and the inherited class 'RegularEmployee'. We have designed the Employee class (generic class) to specify what functionality the derived classes should have. So, an abstract class always serves as the base class for other classes.

We have inherited RegularEmployee class from the Employee class to maintain RegularEmployee data, who is a particular case of employee. This class can use the property procedures and functionality of the Employee class and implement specific functionality related to regular employee. The RegularEmployee class has one data member salary which specifies the regular employee's salary.

We create an object of RegularEmployee class as shown below.

Dim myRegularEmp As RegularEmployee = New RegularEmployee("Paul", "John", 10000)

From the constructor of RegularEmployee class we call the Employee class constructor, which takes two arguments, using the 'MyBase' keyword. By passing the values of fname and lname to the base constructor, we can initialize the data member's of base class - fname and lname.

Code reuse

The class Employee is an abstract class having a constructor to initialize its member variables. The RegularEmployee class is derived from Employee class which is inheriting all the member variables and there is no need to repeat the code for the member variables. This applies to all sub classes which are inheriting from Employee class.

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

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