Microsoft Docs > .NET Languages > Visual Basic > Abstract Classes

Abstract Classes in VB.NET

Abstract classes in Visual Basic .NET are a fundamental concept in object-oriented programming, allowing you to define base classes that cannot be instantiated directly but serve as blueprints for derived classes. They are crucial for enforcing a common structure and behavior across a hierarchy of related classes.

What is an Abstract Class?

An abstract class is a class that is declared with the MustInherit keyword. It can contain both abstract members (methods, properties, events) and non-abstract members. Abstract members are declared with the MustOverride keyword and do not provide an implementation. Derived classes that inherit from an abstract class must provide an implementation for all inherited abstract members, unless they are also declared as abstract.

Key Characteristics:

Declaring an Abstract Class

To declare an abstract class in VB.NET, use the MustInherit keyword before the Class keyword:


Public MustInherit Class Shape
    ' Abstract members
    Public MustOverride Function GetArea() As Double
    Public MustOverride Function GetPerimeter() As Double

    ' Concrete members
    Public Property Color As String
    Public Sub Display()
        Console.WriteLine($"This is a shape with color: {Color}")
    End Sub
End Class
            

Implementing Abstract Members

A concrete class that inherits from an abstract class must implement all of its abstract members using the Overrides keyword.


Public Class Circle
    Inherits Shape

    Public Property Radius As Double

    ' Implementation of abstract member
    Public Overrides Function GetArea() As Double
        Return Math.PI * Radius * Radius
    End Function

    ' Implementation of abstract member
    Public Overrides Function GetPerimeter() As Double
        Return 2 * Math.PI * Radius
    End Function
End Class

Public Class Rectangle
    Inherits Shape

    Public Property Width As Double
    Public Property Height As Double

    ' Implementation of abstract member
    Public Overrides Function GetArea() As Double
        Return Width * Height
    End Function

    ' Implementation of abstract member
    Public Overrides Function GetPerimeter() As Double
        Return 2 * (Width + Height)
    End Function
End Class
            

When to Use Abstract Classes:

Important Note: Abstract classes are similar to interfaces in that they define a contract that derived classes must adhere to. However, abstract classes can also provide partial implementations and contain instance members (fields), which interfaces cannot.

Abstract vs. Interfaces

While both abstract classes and interfaces promote polymorphism and code reuse, they have key differences:

Abstract classes are a powerful tool for building robust and maintainable object-oriented applications in VB.NET. By defining a common structure and enforcing implementation of essential behaviors, they contribute significantly to code organization and the principles of abstraction.