Can you use multiple inheritance with VB.NET?

In C++, a class can derive from multiple base classes. This first installment of .NET Mentor looks at whether the same is true of VB.NET.

Welcome to the maiden voyage of .NET Mentor, a monthly column in which’s contributors will answer your questions about C#, VB.NET, ASP.NET, and anything else related to .NET development. To submit a question, simply drop us an e-mail.

This month’s question comes from member shaunn:

One thing I have noticed is that C# allows multiple inheritance, but I am able to use only deep inheritance with VB.NET. Is it possible to use multiple inheritance with VB.NET, or are VB.NET programmers still just short of the mark?

Shaunn may have been thinking of C++ when he posted his question. C++ supports multiple inheritance, but C# and VB.NET don't. They follow the Java path by allowing inheritance from only a single base class. An inheritance relationship signifies an IS A relationship between two classes. The lack of multiple inheritance becomes a factor when considering it is possible for a class to be classified through multiple IS A relationships. As an example, let’s take three classes, called Employee, USPresident, and USCitizen. The relationship among these classes follows (see Figure A):
  • USPresident IS A Employee
  • USPresident IS A USCitizen

Figure A
In C++, USPresident can derive from multiple bases classes; in .NET languages, it can't.

If multiple inheritance were supported, USPresident would inherit from both Employee and USCitizen. But in .NET languages, you must leverage interfaces to denote multiple IS A relationships. Just like Java, a class can inherit from one base class, and a class can implement multiple interfaces.

The interface
According to the .NET Framework documentation (C# language specification), the interface keyword declares a reference type that has abstract members. The term members in this context denotes a method, property, or indexer, while the term abstract means that no implementation is provided. When creating an interface, the convention is to prefix the name of the interface with the letter I. Although nothing prevents you from ignoring this convention, you will find that it is especially useful to differentiate base classes from interfaces. In Java, developers suffix interfaces with able. Personally, I like to combine these techniques for my interfaces. The jargon used when describing the relationships between classes and interfaces is as follows: We say “x inherits from class y” to signify inheritance, and we say “x implements interface y” to indicate an interface relationship. The colon (:) is the operator for the implementing interfaces.

Inheritance code has the following form:

Class Derived Inherits Base

Class Derived: Base

The code for interface implementation looks like this:

Class Derived
Implements IBase1, IBase2

Class Derived: IBase1, IBase2

Returning to the initial scenario involving the Employee, USPresident, and USCitizen classes, we have two potential base classes: Employee and USCitizen. At this point, which should be the interface and which should be the base class? To answer this question, you need a firm understanding of what inheritance provides that interface implementation does not. The primary difference is that the derived class automatically receives all nonprivate members with inheritance. The term member for a class is different from member in an interface.

What would you like to know?
Please send your .NET development questions to the editors.


Editor's Picks