VKinfotek Inc.
  • Us
    About Us
    Contact Us
  • FAQs
  • Ready Software
    Ready ERP ASP Core Software for Azure Cloud
    Ready Web ERP Software
    Ready ASP.Net Azure Software
    Ready C# SQL Server Accounting Software
    Ready ASP.Net MVC and EF Software
First slide
Earn $100/hour in USA! Click Here

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



Object Oriented Programming Articles
  • Abstract class constructors
  • abstract class inheritance in c#.net
  • abstract class inheritance in visual basic.net
  • New C# Language Feature: Automatic Properties
  • Calling base class constructor in C#
  • base keyword in c#
  • Compile time polymorphism - Method OverLoading
  • How to initialize Base class Object from inside Derived Class constructor
  • Constructor overloading in vb.net
  • How to use inheritance in c#
  • How to use inheritance in visual basic.net
  • Parameter array in vb.net
  • Procedure OverLoading in vb.net
  • Visual Inheritance with Windows Forms
  • Azure Platform
  • GridView CommandField example
  • GridView and DetailsView Master/Detail page using SqlDataSource control
  • POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
  • Query entity data model using linq to entities
  • Difference between arraylist and list collection
  • How to create a Web service using Visual Studio.net
  • FormView DataBound Event
  • Calling base class constructor in C#
  • Convert a sequence to a generic list using ToList()method
  • Project ideas for students
  • Accounting Software
  • Creating an ASP.Net MVC 3 application
  • Using assemblies in .net applications
  • How to implement form validation using ASP.Net 2.0 Validation Controls
  • Constructors in Visual Basic.Net
People also viewed: cover image of book develop erp software using asp core and deploy to Azure Create your own ERP Software using ASP Core>>
cover image of azure cloud book Create your own Azure SOM Software>>
cover image of asp.net book Create your own ERP Software using ASP .Net and SQL>>
cover image of foundation database programming book Create your own Accounting Software using C# >>
cover image of entity framework book Create your own SOM using Entity Framework MVC >>


  • SITEMAP
  • Terms of use
  • Testimonials

Back to top

Copyright © 2016 - All Rights Reserved - VKInfotek.com