The main idea for creating a MustInherit class
is to lay a foundation that other classes can exploit. When creating the
members of such a class, you can prepare them to be overridden. You have
the option of creating overridable and non-overridable members. You will
make the decision based on your requirements.
Sometimes when creating a particular member, you may
intend all derived classes to implement their own version of the member.
You must clearly indicate that any class that wants to inherit from the MustInherit
class must (also) override a particular member. Such a member must be marked with
the MustOverride keyword. To do this, when creating the member,
precede its type with the MustOverride keyword. Here is an example:
Public MustInherit Class Quadrilateral
Public MustOverride Property Area() As Double
End Class
In the same way, you can add as many members as
necessary. You will mark as MustOverride those of your choice and
you can create others without MustOverride. Here are examples:
Public MustInherit Class Quadrilateral
Public Function ShowDescription() As String
Return "Geometric Shape"
End Function
Public MustOverride Property Area() As Double
End Class
After creating a MustInherit class, you can inherit new
classes from it using the Inherits keyword we saw in the previous lessons.
Here is an example:
Public Class Square
Inherits Quadrilateral
End Class
When deriving a class from a MustInherit, the
first rule you must observe is that each member of the abstract that was
marked as MustOverride must be overridden. Based on this, in our
Square class, you must (at least) implement the Area property. Here is
an example:
Public MustInherit Class Quadrilateral Public Function ShowDescription() As String Return "Geometric Shape" End Function Public MustOverride ReadOnly Property Area() As Double End Class Public Class Square Inherits Quadrilateral Public Overrides ReadOnly Property Area() As Double Get Return 0 End Get End Property End Class
In the derived class, as a new one, you can add new
members as you judge them necessary. Here are examples:
Public MustInherit Class Quadrilateral
Public Function ShowDescription() As String
Return "Geometric Shape"
End Function
Public MustOverride ReadOnly Property Area() As Double
End Class
Public Class Square
Inherits Quadrilateral
Public sd As Double
Public Sub New()
sd = 0
End Sub
Public Sub New(ByVal side As Double)
sd = side
End Sub
Public Property Side() As Double
Get
Return sd
End Get
Set(ByVal Value As Double)
If Value <= 0 Then
sd = 0
Else
sd = Value
End If
End Set
End Property
Public Overrides ReadOnly Property Area() As Double
Get
Return sd * sd
End Get
End Property
End Class
After deriving a new class from a MustInherit
class, you can declare a variable of it and instantiate it
using the New operator. Here is an example:
<%@ Page Language="VB" %> <html> <head> <script language="VB" runat="server"> Public MustInherit Class Quadrilateral Public Function ShowDescription() As String Return "Geometric Shape" End Function Public MustOverride ReadOnly Property Area() As Double End Class Public Class Square Inherits Quadrilateral Public sd As Double Public Sub New() sd = 0 End Sub Public Sub New(ByVal side As Double) sd = side End Sub Public Property Side() As Double Get Return sd End Get Set(ByVal Value As Double) If Value <= 0 Then sd = 0 Else sd = Value End If End Set End Property Public Overrides ReadOnly Property Area() As Double Get Return sd * sd End Get End Property End Class </script> <title>Exercise</title> </head> <body> <% Dim sqr As Square = New Square sqr.Side = 35.75 Response.Write(" -=- Square Characteristics -=-<br />") Response.Write("Description: " & sqr.ShowDescription & "<br />") Response.Write("Side: " & sqr.Side & "<br />") Response.Write("Area: " & sqr.Area) %> </body> </html>
This would produce:
As mentioned earlier, after creating a MustInherit
class, you can declare a variable of it but you cannot instantiate it
using the New operator. Consider the following example:
<% Dim sqr As Quadrilateral sqr = New Square %>
These declaration and instantiation are legal but the
(tri) variable gives you access only to members that are present in the
parent class. This means that this declaration gives you access to the
ShowDescription() method and the Area property of the Quadrilateral class.
When instantiating a class derived from a MustInherit
class, if you want to access its members, you must apply its name to the New
operator as we saw in the last example of the previous section. Here is an example:
<%
Dim sqr As Square = New Square(42.68)
Response.Write(" -=- Square Characteristics -=-")
Response.Write("Description: " & sqr.ShowDescription)
Response.Write("Side: " & sqr.Side)
Response.Write("Area: " & sqr.Area)
%>
All of the classes we have used so far can serve as
parents of other classes. This is the default behavior of a regular class:
the ability to derive a new class from it. In some cases, you may not want
any class to be derived from a particular class you are creating. Such a
class is referred to as sealed.
|
Class Abstraction
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment