Abstract classes and Interfaces
If you are new to VB.Net School
This is the 7th in the series of lessons in the VB.Net School. The VB.Net School is a kind of interactive learning platform where those who want to learn .NET with VB.Net can find help and support. With one issue a week, describing some areas of the VB.Net Programming Language with the Microsoft .Net Platform, this is not the same traditional passive tutorial where the author only writes and the reader only reads. There will be exercise problems at the end of each issue, which the reader is expected to solve after reading the issue. The solution to these problems will be provided in the next issue for testing purposes. There is also a dedicated
message board attached with the school, where you can ask questions about the article, and the author will respond to your question within 2/3 days. You can send your suggestions, feedback or ideas on how these lessons can be improved to either the Author (
farazrasheed@acm.org) or the WEBMASTER (
info@programmersheaven.com).
For previous lessons click
here
Lesson Plan
In this lesson we will explore abstract (
MustInherit) classes and interfaces. We will look at the idea behind the abstract (
MustOverride) methods, abstract classes (
MustInherit), interfaces and how they are implemented in VB.Net. Later we will see how to cast to and from the interface reference by using the
TypeOf...is and
CType() operators
Abstract (MustInherit) Classes
Abstract classes can simply defined as incomplete classes.
- Abstract (MustInherit) classes contain one or more incomplete methods called abstract (MustOverride) methods.
- The abstract (MustInherit) class only provides the signature or declaration of the abstract (MustOverride) methods and leaves the implementation of these methods to the derived or sub-classes.
- Abstract classes are marked with MustInherit and abstract methods are marked with the MustOverride keyword
- Since abstract classes are incomplete; they can not be instantiated. They must be sub-classed in order to use their functionality. This is the reason why an abstract class can't be NotInheritable
- A class inheriting an abstract class must implement all the abstract methods in the abstract class or it must also be declared as an MustInherit class.
- A class inheriting an abstract class and one that implements all its abstract methods is called a concrete class of that abstract class.
- We can declare a reference of the type of abstract class and it can point to the objects of classes that have inherited the abstract class.
Let us declare an abstract class with two concrete properties and an incomplete (
MustOverride) method.
MustInherit Class TaxCalculator
Protected mItemPrice As Double
Protected mTax As Double
' an abstract (MustOverride) function
Public MustOverride Function CalculateTax() As Double
' Two concrete properties
Public ReadOnly Property Tax() As Double
Get
Return Tax
End Get
End Property
Public ReadOnly Property ItemPrice() As Double
Get
Return ItemPrice
End Get
End Property
End Class
The abstract (
MustInherit)
TaxCalculator class contains two fields:
mItemPrice and applied tax (
mTax). It contains an abstract
MustOverride function
CalculateTax() which calculates the tax applied on the
mItemPrice and stores it in the field
mTax. The
CalculateTax() function is made abstract so the concrete sub-classes can provide their own criteria for applying the
tax on the
itemPrice. The class also contains two public read only properties to access the two private fields. If we try to instantiate this abstract class in the
Main() method
Public Sub Main()
Dim taxCalc As New TaxCalculator()
End Sub
The compiler will complain as so
'New' cannot be used on class 'TaxCalculator' because it contains a 'MustOverride' member that has not been overridden.
In order to create an instance of the
TaxCalculator class, we need to sub-class it. Let us now inherit a class from the MustInherit
TaxCalculator class and call it
SalesTaxCalculator
Class SalesTaxCalculator
Inherits TaxCalculator
Public Sub New(ByVal pItemPrice As Double)
Me.mItemPrice = pItemPrice
End Sub
Public Overrides Function CalculateTax() As Double
mTax = 0.3 * mItemPrice
Return mItemPrice + mTax
End Function
End Class
The
SalesTaxCalculator class inherits
TaxCalculator and overrides its
CalculateTax() functions. It applies 30% tax on the price of item (a bit harsh!) and returns the new price of the item. The
SalesTaxCalculator class also defines a constructor that takes the
itemPrice as its parameter. If we don't provide the implementation of the
CalculateTax() method in
SalesTaxCalculator
Class SalesTaxCalculator
Inherits TaxCalculator
Public Sub New(ByVal pItemPrice As Double)
Me.mItemPrice = pItemPrice
End Sub
'Public Overrides Function CalculateTax() As Double
' mTax = 0.3 * mItemPrice
' Return mItemPrice + mTax
'End Function
End Class
We will get a compile time error as
Class 'SalesTaxCalculator' must either be declared 'MustInherit' or override the following inherited 'MustOverride' member(s): Public MustOverride Function CalculateTax() As Double.
OK, un-comment the overridden
CalculateTax() method in
SalesTaxCalculator. Since we have overridden the
CaculateTax() method of
TaxCalculator in the
SalesTaxCalculator class, we can create its instance in the
Main() method
Public Sub Main()
Dim salesTaxCalc As New SalesTaxCalculator(225)
Dim newPrice As Double = salesTaxCalc.CalculateTax()
Console.WriteLine("The item price changed because of sales _
tax from {0} $ to {1} $", _
salesTaxCalc.ItemPrice, newPrice)
Console.WriteLine("Tax applied = {0} $", salesTaxCalc.Tax)
End Sub
Here we instantiated the
SalesTaxCalculator class just like a regular class and accessed its members. The output of the above program will be
The item price changed because of sales tax from 225 $ to 292.5 $
Tax applied = 67.5 $
Press any key to continue
We can also use the abstract class type (
TaxCalculator) reference to handle the object of its concrete class (
SalesTaxCalculator) in our
Main() method
Public Sub Main()
Dim salesTaxCalc As TaxCalculator = New SalesTaxCalculator(225)
Dim newPrice As Double = salesTaxCalc.CalculateTax()
Console.WriteLine("The item price changed because of _
sales tax from {0} $ to {1} $", _
salesTaxCalc.ItemPrice, newPrice)
Console.WriteLine("Tax applied = {0} $", salesTaxCalc.Tax)
End Sub
We can derive as much concrete classes as we want from the abstract
TaxCalculator class as long as they provide the definition of the abstract (
MustOverride) methods of it. Here is another concrete class (
WarSurchargeCalculator) of the abstract
TaxCalculator class.
Class WarSurchargeCalculator
Inherits TaxCalculator
Public Sub New(ByVal pItemPrice As Double)
Me.mItemPrice = pItemPrice
End Sub
Public Overrides Function CalculateTax() As Double
mTax = 0.5 * mItemPrice
Return mItemPrice + mTax
End Function
End Class
The
WarSurchargeCalculator can be used similarly in the
Main() method.
School Home