Polymorphism Using Abstract Classes in Visual Basic
Polymorphism Using Abstract Classes in Visual Basic
This article gives you an in-depth understanding of abstract methods, abstract classes and
polymorphism using abstract classes in Visual Basic 2005.
If you are new to OOP in Visual Basic.NET, I strongly suggest that you go through the following
links:
Using Constructors with Object-Oriented Database Development
Properties and Object-Oriented Database Development
Using Methods with Object-Oriented Database Development
An Introduction to Object-Oriented Database Development
The entire source code for this article is available in the form of a downloadable zip file. The
solution was developed using Microsoft Visual Studio 2005 Professional Edition on Microsoft
Windows Server 2003 Enterprise Edition. I didn't really test it in any other environment. I
request that you post in the discussion area if you have any problems in execution.
Life without the "Abstract" class in Visual Basic 2005
Before understanding the "Abstract" class, let us go through an example with a set of classes,
which demonstrates the inconveniences of repeating the code.
The following is a class named "Rectangle" defined with length and breadth as properties along
with a method named "GetArea" to calculate the area of a rectangle:
Public Class Rectangle
Private _l As Double
Private _b As Double
End Class
There exists nothing special about the previous class apart from properties, methods and a
constructor. Let us consider two more classes, "EquiTriangle" and "Square," which have
properties that are similar to those of our "Rectangle" class. This will be continued in the next
section.
Public Sub New(ByVal breadth As Double, ByVal height As Double) 'code removed for clarity
A method which has only a signature (declaration) without any implementation can be called an
abstract method. Such methods, when declared, must be defined with "MustOverride." The
abstract methods need to be overridden and implemented in child classes with the same signature
that was defined in the parent classes.
Any class which has at least one abstract method must be called as an abstract class. The
abstract class must be defined with "MustInherit" if it has at least one abstract method. Note that
the abstract class may contain several methods; a few of those may already be implemented (not
abstract methods) and a few may not (abstract methods). It doesn't mean that abstract classes
cannot contain any methods with implementation.
Let us define an abstract class as follows:
Public MustInherit Class Shape
Private _x As Double
Private _y As Double
End Sub
End Class
The above class is named "Shape" and is defined with "MustInherit;" that means it may have one
or more abstract methods. Besides normal members such as constructors, properties, fields, and
so forth, it has a method declared as follows:
Public MustOverride Function GetArea() As Double
The above method is declared and defined without any code (or implementation). It is simply
declared with "MustOverride." Any method declared as "MustOverride" is an abstract method. A
class must be defined as "MustInherit" if it has at least one "MustOverride" (abstract) method.
The next section deals with the implementation of abstract methods.
In the previous section, a class named "Shape" is defined with "MustInherit" as it has one
method declared with "MustOverride" (an abstract method). In this section, I shall create child
classes (sub classes) for the parent class (super class) "Shape."
Let us start with the following:
Public Class Rectangle
Inherits Shape
End Class
In the above "Rectangle" class, we have a constructor with the following statement:
MyBase.New(l, b)
The above statement calls the constructor of the super class; in this case, it is constructor of
"Shape." Finally, the abstract method is implemented with the same signature defined in the
"Shape" class as follows:
Public Overrides Function GetArea() As Double
Return Me.X * Me.Y
End Function
Similarly, the "EquiTriangle" and "Square" can be defined as described in the next section.
Continuing from the previous section, I defined two more sub classes for the super class "Shape"
as follows:
Public Class EquiTriangle
Inherits Shape
End Class
If the developer doesn't implement "GetArea" in any of the classes inherited from the "Shape"
class, it would be a compilation error. This is how we can enforce consistency among members
of classes belonging to the same group.
Finally, to test the above classes, we can write the code as follows:
Dim r As New Rectangle(10, 20)
Dim s As New Square(20)
Dim t As New EquiTriangle(5, 10)
MessageBox.Show("Area of rectangle: " & r.GetArea)
MessageBox.Show("Area of square: " & s.GetArea)
MessageBox.Show("Area of triangle: " & t.GetArea)