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.
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 ClassImplementing Abstract Class
Public Class RegularEmployee Inherits Employee End ClassWhen 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
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