MSDN Documentation

Abstraction in Visual Basic

Abstraction is the process of exposing only essential features of a type while hiding its implementation details. In VB.NET you achieve abstraction primarily through MustInherit classes and MustOverride members, as well as interfaces.

When to use abstraction

Creating an abstract (MustInherit) class

Public MustInherit Class Shape
    Public MustOverride Function Area() As Double
    Public Overridable Sub Draw()
        Console.WriteLine("Drawing a shape")
    End Sub
End Class

Public Class Circle
    Inherits Shape
    Private _radius As Double

    Public Sub New(r As Double)
        _radius = r
    End Sub

    Public Overrides Function Area() As Double
        Return Math.PI * _radius * _radius
    End Function

    Public Overrides Sub Draw()
        Console.WriteLine($"Drawing a circle with radius {_radius}")
    End Sub
End Class

Implementing an interface

Public Interface IPrintable
    Sub Print()
End Interface

Public Class Report
    Implements IPrintable

    Public Sub Print() Implements IPrintable.Print
        Console.WriteLine("Printing report...")
    End Sub
End Class

Try it yourself

Enter a radius below to see the area calculated by the Circle class.