DevX HomePage

Using Abstract Classes in Visual Basic.NET

With VB.NET, Visual Basic programmers finally have full object-oriented capabilities at their disposal. One of the things you can do with OOP is create abstract classes. Though this programming tool is often overlooked, abstract classes offer many development possibilities.
or years, Visual Basic programmers pleaded with Microsoft to give them the object-oriented programming (OOP) capabilities that were enjoyed by C++ and Java developers. While a few OOP-related features were added to the past couple of releases, we had to wait for Visual Basic.NET to deliver true OOP to Visual Basic. One of the things you can do with OOP is create abstract classes. This programming tool is often overlooked—which is a shame because abstract classes offer a lot of possibilities to the developer.

What you need:
Some familiarity with the fundamentals of object-oriented programming and with Visual Basic.NET.

It's easy to explain what abstract classes are. They're simply classes that cannot be instantiated. You cannot create an object from such a class for use in your program.

It's trickier to explain why they're interesting. You might think that a class that cannot be instantiated is not much use. Not true. In certain situations, abstract classes can be very useful. You can use an abstract class as a base class, creating new classes that inherit from it. Creating an abstract class with a certain minimum required level of functionality gives you a defined starting point from which you can derive non-abstract classes.

Here's a hypothetical programming situation that would benefit from an abstract class. Suppose your graphics application provides a variety of drawing classes: circle, rectangle, Bézier curve, and so on. All these drawing classes have certain members in common—such as properties for position, size, and color, as well as a method to erase the drawn object. Other class members, such as the method for actually rendering the shape, differ for each individual class (type of shape). This makes an ideal situation for an abstract class.


Figure 1. Possible inheritance structure for an abstract class.

In the abstract class you define all the common class members that are needed by all drawing classes. Because it doesn't correspond to any shape, or have a rendering method, there is no reason for a program ever to instantiate it. But by using it as the base class for all your shape-specific drawing classes, you ensure that all these non-abstract classes have the required basic functionality. Figure 1 shows a possible inheritance structure for this scenario.

Creating an Abstract Class
Defining an abstract class is as simple as including the MustInherit keyword in the class definition:

Public MustInherit Class MyAbstractClass
...
End Class

Aside from this keyword, an abstract class definition is no different from a non-abstract class. It can contain properties and methods, inherit from another class, implement an interface, and so on. However, you must be particularly aware of how class members (properties and methods) that are defined in the abstract class relate to members that can be defined in derived classes. There are no hard and fast rules here, but the decisions you do make will be related to the functionality of your class and its planned derived classes.

You have three choices:

Abstract Classes vs. Interfaces
If you are familiar with interfaces, you may be thinking that an abstract class does much the same thing. This is only partly true. Defining an abstract class that contains only abstract members has essentially the same effect as defining an interface, because you specify that derived classes must implement certain members with specific signatures. Beyond this, however, abstract classes provide additional capabilities—specifically, the definition of base functionality in the form of non-abstract members, something that an interface cannot do.

When you think that either an abstract class or an interface would work, keep these considerations in mind:

Depending on the needs of your project, you may rely on a single abstract class, one or more interfaces, or a combination of an abstract class with an interface. Visual Basic and .NET provide a great deal of flexibility in this regard, and you can often achieve the same result in more than one way. The bottom line is that there are some things that just cannot be done without using an abstract class. Abstract classes may be thought of as a rather specialized programming tool. When the situation calls for it, they reduce the developer's work and lead to a simpler, more robust application.

An Abstract Class in Action
Imagine developing a new employee records system for a large multinational corporation. Your job is to supervise the implementation of classes to represent employees such that certain core requirements of the head office are met, while still providing flexibility for differing requirements from company branches elsewhere.

The core requirements are as follows:

The code for the resulting abstract class, called EmployeeBase, is shown in Listing 1. You'll see that I've omitted the actual implementation of the base members because those details are not relevant here.

At the company office in Paris, France, a programmer is using the EmployeeBase class as the basis for the EmployeeFrance class (see Listing 2) to use with the local employee records software. This derived class will necessarily inherit the Name and DateOfHire members. Furthermore, the RetirementID member in the EmployeeBase class is suitable for use in France, so the new class will not override it. All that the programmer has to do is implement a method to override the abstract Compensation member.

In London, England, however, the base class's RetirementID property is not suitable, so the derived class (called EmployeeEngland, shown in Listing 3) will override that member as well as the Compensation member. The implementation of Compensation will likely be different from the one used in France.

Peter G. Aitken has been writing about computers and programming for over 10 years, with some 30 books and hundreds of articles to his credit. Recent book titles include Developing Office Solutions With Office 2000 Components and VBA, Windows Script Host, and the soon to be published XML the Microsoft Way. He is a regular contributor to OfficePro magazine, and for several years was a contributing editor for Visual Developer Magazine where he wrote the popular Visual Basic column. Peter is the proprietor of PGA Consulting, providing custom application and Internet development to business, academia, and government since 1994.
DevX is a division of Internet.com.
© Copyright 2010 Internet.com. All Rights Reserved. Legal Notices