ARTICLE

Abstract Classes in VB.NET

Posted by Rajesh VS Articles | Visual Basic 2010 June 13, 2003
This is a detailed analysis of abstract classes and methods in VB.NET with some concrete examples.
 
Reader Level:

This is a detailed analysis of Abstract classes and methods in VB.NET with some concrete examples.

The keyword abstract can be used with both classes and methods in VB.NET to declare them as abstract.

The classes, which we can't initialize, are known as abstract classes. They provide only partial implementations. But another class can inherit from an abstract class and can create their instances.

For example, an abstract class with a non-abstract method.

Imports System
MustInherit Class MyAbs
Public Sub NonAbMethod()
Console.WriteLine("Non-Abstract Method")
End Sub 'NonAbMethod
End Class 'MyAbs
Class MyClass
Inherits MyAbs
End Class 'MyClass]
Class MyClient
Public Shared Sub Main()
'MyAbs mb = new MyAbs();//not possible to create an instance
Dim mc As New MyClass()
mc.NonAbMethod()
' Displays 'Non-Abstract Method'
End Sub 'Main
End Class 'MyClient

An abstract class can contain abstract and non-abstract methods. When a class inherits from an abstract, the derived class must implement all the abstract methods declared in the base class.

An abstract method is a method without any method body. They are implicitly virtual in VB.

Imports System
MustInherit Class MyAbs
Public Sub NonAbMethod()
Console.WriteLine("Non-Abstract Method")
End Sub 'NonAbMethod
Public MustOverride Sub AbMethod() ' An abstract method
End Class 'MyAbs
Class MyClass
Inherits MyAbs
'must implement base class abstract methods
Public Overrides Sub AbMethod()
Console.WriteLine("Abstarct method")
End Sub 'AbMethod
End Class 'MyClass
Class MyClient
Public Shared Sub Main()
Dim mc As New [MyClass]
mc.NonAbMethod()
mc.AbMethod()
End Sub 'Main
End Class 'MyClient

But by declaring the derived class also abstract, we can avoid the implementation of all or certain abstract methods. This is what is known as partial implementation of an abstract class.

Imports
System
MustInherit Class MyAbs
Public MustOverride Sub AbMethod1()
Public MustOverride Sub AbMethod2()
End Class 'MyAbs
'not necessary to implement all abstract methods
'partial implementation is possible
MustInherit Class MyClass1
Inherits MyAbs
Public Overrides Sub AbMethod1()
Console.WriteLine("Abstarct method #1")
End Sub 'AbMethod1
End Class 'MyClass1
Class [MyClass]
Inherits MyClass1
Public Overrides Sub AbMethod2()
Console.WriteLine("Abstarct method #2")
End Sub 'AbMethod2
End Class '[MyClass]
Class MyClient
Public Shared Sub Main()
Dim mc As New [MyClass]
mc.AbMethod1()
mc.AbMethod2()
End Sub 'Main
End Class 'MyClient

In VB, an abstract class can inherit from another non-abstract class. In addition to the methods it inherited from the base class, it is possible to add new abstract and non-abstract methods as showing below.

Imports System
Class MyClass1
' Non-Abstract class
Public Sub Method1()
Console.WriteLine("Method of a non-abstract class")
End Sub 'Method1
End Class 'MyClass1
MustInherit Class MyAbs
Inherits MyClass1
' Inherits from an non-abstract class
Public MustOverride Sub AbMethod1()
End Class 'MyAbs
Class MyClass
Inherits MyAbs
'must implement base class abstract methods
Public Overrides Sub AbMethod1()
Console.WriteLine("Abstarct method #1 of MyClass")
End Sub 'AbMethod1
End Class 'MyClass
Class MyClient
Public Shared Sub Main()
Dim mc As New MyClass()
mc.Method1()
mc.AbMethod1()
End Sub 'Main
End Class 'MyClient

An abstract class can also implement from an interface. In this case we must provide method body for all methods it implemented from the interface.

Imports System
Interface IInterface
Sub Method1()
End Interface 'IInterface
MustInherit Class MyAbs
Implements IInterface 'ToDo: Add Implements Clauses for implementation methods of these interface(s)
Public Sub Method1()
Console.WriteLine("Method implemented from the IInterface")
End Sub 'Method1
End Class 'MyAbs
Class MyClass
Inherits MyAbs 'must implement base class abstract method
End Class 'MyClass
Class MyClient
Public Shared Sub Main()
Dim mc As New [MyClass]
mc.Method1()
End Sub 'Main
End Class 'MyClient '

We can't use the key word abstract along with sealed in VB, since a sealed class can't be abstract.

The abstract methods are implicitly virtual and hence they can't mark explicitly virtual in VB.

For example

Imports System
MustInherit Class MyAbs
Public MustOverride Sub AbMethod1()
Public MustOverride Sub AbMethod2()
End Class 'MyAbs
Class MyClass1
Inherits MyAbs
Public Overrides Sub AbMethod1()
Console.WriteLine("Abstarct method #1 of MyClass1")
End Sub 'AbMethod1
Public Overrides Sub AbMethod2()
Console.WriteLine("Abstarct method #2 of MyClass1")
End Sub 'AbMethod2
End Class 'MyClass1
Class MyClient
Public Shared Sub Main()
Dim ma1 = New MyClass1 ' Polymorphism
ma1.AbMethod1()
ma1.AbMethod2()
End Sub 'Main
End Class 'MyClient

Login to add your contents and source code to this article
share this article :
post comment
 

hi rajesh i hav jst joined the it industry,hav much intrested & knowledgein java; but i got break for .net in a local com. of pune. please give me some suggestions 1)why all big companies work in java only;not in .net. 2)whats the growth oppurtunity in .net with respect to java. in particularly .net where i hav to concentrate more waiting for ur reply. gaurav mail id-- eminence.gaurav@gmail.com

Posted by kumar gaurav Oct 25, 2007
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor