Home

Class Abstraction

 

An Abstract Class

 

Introduction

It should appear to us clearly by now that inheritance is useful by allowing us to use an object that was already created but "upgrade" it with new features that were not available when the original object was born. In most cases, when creating a class, you may not thing that other classes would be inherited from it. In fact, this will usually not be your concern: you simply create a class and use it as needed.

In some other cases, rather as you build your experience with Visual Basic. You may create a class that, although useful at the time, you may already think of other classes that would be based on it. This means that, at the time you are creating such a class, you would already keep inheritance in mind. Visual Basic provides various features that can assist you with creating and implementing class with different goals in mind.

Abstracting a Class

Imagine that, when creating a class, you already find that it is too or highly generalized to serve one particular purpose. As an example, imagine you start creating a class you intend to use to process calculations for a rectangle, or a square, or a parallelogram, or even a triangle:

Rectangle Square Parallelogram Triangle

You start thinking that the same class could be used for different types of geometric shapes. In this case, instead of creating a different class for each shape, you can create a generalized class that these shapes can be based on. Unfortunately, these shapes, although each characterized as geometric, don't have much in common; otherwise they would not be different. One of their common characteristics is that each has a name. While the parallelogram and the triangle have a base, the rectangle and the square don't explicitly have one. Also, neither the perimeter nor the area of these shapes are calculated the same. Still, as long as you find at least one characteristic that these objects have, you can create a class that would share. In other words, you can create a class that features one or more characteristics that these objects have. Then each object would customize its behavior(s) based on its particular characteristics. This is the basis of abstraction.

A class is referred to as abstract when it is only used to lay a foundation for other classes. In the Microsoft Visual Basic language, to create an abstract class, you must precede its Class keyword with MustInherit. Here is an example:

Public MustInherit Class Quadrilateral

End Class

After creating a class and marking it as MustInherit, you can add one or more members to it, as done in the previous examples we used so far. Here is an example:

Public MustInherit Class Quadrilateral
    Public Function ShowDescription() As String
        Return "Geometric Shape"
    End Function
End Class

If you create a class and mark it as MustInherit, it is considered incomplete. Because of that, although you can declare a variable of that type, you cannot initialize its instance using the New operator. Consider the following 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
End Class
</script>

<title>Exercise</title>

</head>
<body>

<%    
    Dim quad As Quadrilateral = New Quadrilateral

    Response.Write("Description = " & quad.Description)
%>

</body>
</html>

This would produce an error because you cannot use New to instantiate a MustInherit class

Error

This means that, before using a MustInherit class, you must derive a class from it. 

 

 

 

Overriding Members of an Abstract Class

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:

Class

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)
%>

Sealed Classes

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.

A class is said to be sealed when you cannot inherit from it. If you try, you would receive an error. To create a sealed class in Microsoft Visual Basic, precede the name of the class with the NotInheritable keyword.

 
 
   
 

Previous Copyright © 2009-2013 FunctionX, Inc., Inc. Home