Polymorphism using Abstract Classes in Visual Basic.NET 2005 - Defining and implementing abstract classes in Visual Basic 2005: abstract class
(Page 3 of 6 )
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
Public Sub New()
End Sub
Public Sub New(ByVal SingleValue As Double)
Me.X = SingleValue
End Sub
Public Sub New(ByVal FirstValue As Double, ByVal SecondValue As Double)
Me.X = FirstValue
Me.Y = SecondValue
End Sub
Public Property X() As Double
Get
Return _x
End Get
Set(ByVal value As Double)
_x = value
End Set
End Property
Public Property Y() As Double
Get
Return _y
End Get
Set(ByVal value As Double)
_y = value
End Set
End Property
Public MustOverride Function GetArea() As Double
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.
Next: Defining and implementing abstract classes in Visual Basic 2005: child class >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee