Inheritance and Polymorphism in VB.NET
In This Chapter
- Inheritance Basics
- What Is Inheritance?
- Defining Classes That Must Be Subclassed
- Defining Classes That Cannot Be Subclassed
- Polymorphism
- Dynamic Typecasting
- Defining Interfaces
- Summary
Visual Basic .NET supports inheritance. VB6 did not. VB6 supported interface implementation, which is a facility of the Component Object Model (COM). Why Microsoft chose to implement COM and interfaces before classes and inheritance is anybody's guess, but Visual Basic .NET supports both interfaces and inheritance. The net result is that Visual Basic .NET is significantly more powerful and has features and capabilities consistent with the most advanced languages available today.
Through version 6, we said that Visual Basic was object-based rather than object- oriented. The primary missing ingredient was inheritance. Visual Basic .NET rectifies this deficit.
Chapter 10 demonstrates how to use inheritance in Visual Basic .NET, as well as the new capabilities associated with inheritance relationships. Additionally there are revisions to the way interfaces are supported in Visual Basic .NET. Chapter 10 will demonstrate inheritance, virtual abstract classes, final classes, polymorphic behavior, dynamic typecasting, and how to define and implement interfaces in Visual Basic .NET.
Inheritance Basics
Interfaces contain declarations only. Classes can contain both declarations and definitions. Classes can implement interfaces as well as inherit from a single parent class. Interfaces use the keyword Interface, and classes use the keyword Class in Visual Basic .NET.
The difference between an interface and a class is that classes provide implementations for their members and interfaces define members without implementations. Both classes and interfaces support inheritance. A class may inherit from a single parent and implement zero or more interfaces, and interfaces support multiple interface inheritance but provide no implementation. (We will return to interfaces later in this chapter. For now let's focus on what classes offer.) Even if your understanding of inheritance is expert, you are encouraged to read the remainder of this section simply to see the grammar used to support class inheritance in Visual Basic .NET.