Visual Basic Abstraction

In visual basic, Abstraction is a principle of object-oriented programming language (OOP) and it is useful to hide the implementation details and display only the essential features of the object.

 

In Abstraction, by using access modifiers we can hide the required details of the object and expose only the necessary methods and properties through the reference of the object.

 

In real-time, laptop is the perfect example of abstraction in visual basic. A laptop that consists of many things such as processor, RAM, motherboard, LCD screen, camera, USB ports, battery, speakers, etc. To use it, we just need to know how to operate the laptop by switching it on, we don’t need to know how internally all the parts are working. Here, the laptop is an object which is designed to expose only the required features by hiding its implementation details.

 

In object-oriented programming, class is the perfect example of abstraction. In visual basic, we can create a class with required methodsproperties and we can expose only necessary methods and properties using access modifiers based on our requirements.

 

Following is the example of defining a class with required methodsproperties and exposing it by using access modifiers to achieve abstraction functionality.

 

Public Class Laptop

    Private brand As String

    Private model As String

    Public Property Lbrand() As String

        Get

            Return brand

        End Get

        Set(ByVal value As String)

            brand = value

        End Set

    End Property

    Public Property Lmodel() As String

        Get

            Return model

        End Get

        Set(ByVal value As String)

            model = value

        End Set

    End Property

    Public Sub LaptopDetails()

        Console.WriteLine("Brand: " & Lbrand)

        Console.WriteLine("Model: " & Lmodel)

    End Sub

    Public Sub LaptopKeyboard()

        Console.WriteLine("Type using Keyword")

    End Sub

    Private Sub MotherBoardInfo()

        Console.WriteLine("MotheBoard Information")

    End Sub

    Private Sub InternalProcessor()

        Console.WriteLine("Processor Information")

    End Sub

End Class

If you observe the above code, we defined a Laptop class with required fieldsproperties and methods with Public, Private access modifiers to achieve an abstraction functionality by hiding and exposing some of the methods and properties based on our requirements.

 

Here, the Public modifier is useful to allow defined fieldsproperties and methods to access outside of the class and the Private modifier is useful to hide or restrict access of required fieldsproperties and methods from the outside of class.

 

By creating an instance of Laptop class we can access the defined fieldsproperties and methods. Following is the pictorial representation of creating an instance of a class and accessing fieldsproperties and methods.

 

Visual Basic Abstraction Example Representation Diagram 

If you observe the above diagram, we are exposing only the necessary properties and methods outside of the class by using Public and Private access modifiers.

Visual Basic Abstraction Example

Following is the example of implementing an abstraction functionality by allowing only a few properties and methods to access outside of the class in a visual basic programming language.

 

Module Module1

    Public Class Laptop

        Private brand As String

        Private model As String

        Public Property Lbrand() As String

            Get

                Return brand

            End Get

            Set(ByVal value As String)

                brand = value

            End Set

        End Property

        Public Property Lmodel() As String

            Get

                Return model

            End Get

            Set(ByVal value As String)

                model = value

            End Set

        End Property

        Public Sub LaptopDetails()

            Console.WriteLine("Brand: " & Lbrand)

            Console.WriteLine("Model: " & Lmodel)

        End Sub

        Public Sub LaptopKeyboard()

            Console.WriteLine("Type using Keyword")

        End Sub

        Private Sub MotherBoardInfo()

            Console.WriteLine("MotheBoard Information")

        End Sub

        Private Sub InternalProcessor()

            Console.WriteLine("Processor Information")

        End Sub

    End Class

    Sub Main()

        Dim l As Laptop = New Laptop()

        l.Lbrand = "Dell"

        l.Lmodel = "Inspiron 14R"

        l.LaptopDetails()

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we defined fieldsproperties, and methods with Public, Private access modifiers to allow or disallow propertiesmethods access based on requirements.

 

When we execute the above visual basic program, we will get the result as shown below.

 

Visual Basic (VB) Abstraction Example Result

Visual Basic Abstraction vs Encapsulation

The following are the differences between abstraction and encapsulation in a visual basic programming language.

 

AbstractionEncapsulation
It is useful to hide unwanted data and show only the required properties and methods. It is useful to bind data members and member functions into a single unit to prevent outsiders to access it directly.

This is how we can use the abstraction to hide the implementation details and show only essential features based on our requirements in a visual basic programing language.