Abstract Classes in Visual Basic .NET

In this article, I will explain you about Abstract Classes and their implementation in Visual Basic .NET.
  • 2631

In this article, I will explain you about Structures in Visual Basic .NET.

Structures

Structures are complex data types that encapsulate group of logically related data items. Structures are user-defined. They are very similar to Classes. Just like Classes, Structures can contain data members as well as member methods. The main difference in Structures and Classes is that structures are value type and classes are reference type. We use Structure . . . End Structure statement to declare a structure in Visual Basic .NET. Between these two statements, there must be at least one member declared and that member can be of any data type, non-shared and non-event.

In this article, I will explain you about Abstract Classes and their implementation in Visual Basic .NET.

Abstract Classes

The opposite of NotInheritable classes are classes that must be inherited from before they can be used. In Object-Oriented Programming, such classes are referred to as Abstract classes. Abstract classes are closely related to interfaces. Abstract classes have members that must be implemented just like interfaces. The biggest difference is that you can inherit some implementation from abstract classes but interfaces contain no implementation. Unlike interfaces, a class can inherit only one abstract class. Abstract class is designed to act as a base class. In program development abstract classes is a design concept that provides a base upon which other classes are built.

Creating Abstract Classes

An abstract class can use any members like all other classes. Members of an abstract class can have a fixed implementation that will be same to all inheriting members or they can be Overridable. Since abstract classes are incomplete,  they
can not be instantiated. We use MustInherit keyword to create an abstract class in Visual Basic .NET. Abstract classes contain one or more incomplete methods called abstract methods  and they also does not provide any details regarding their implementation. But access level, member type, return type and required parameters are specified. Abstract members were only declared in abstract classes. The MustOverride keyword is use to declare an abstract member.

The following code show you the implementation of Abstract Classes:

Imports System.Console
Module Module1
 
    Public MustInherit Class AbsClass
        'declaring an abstract class with MustInherit keyword
        Public MustOverride Function Sum() As Integer
        Public MustOverride Function Multiply() As Integer
        'declaring two abstract members with MustOverride keyword
    End Class
 
    Public Class AbstractFirst
        Inherits AbsClass
        'implementing the abs class by inheriting
        Dim A As Integer = 40
        Dim B As Integer = 60
        'declaring two integers
 
        Public Overrides Function Sum() As Integer
            Return A + B
        End Function
        'implementing the Sum method
 
        Public Overrides Function Multiply() As Integer
            Return A * B
        End Function
        'implementing the Multiply method
    End Class
 
    Sub Main()
        Dim Obj As New AbstractFirst()
        'creating an instance of AbstractFirst
        WriteLine("Sum of A+B is" & " " & Obj.Sum())
        WriteLine("Multiple of A*B is" & " " & Obj.Multiply())
        'displaying output
        Read()
    End Sub
 
End Module

The output of the above code is:

Output.gif

Summary

Hope this article help you to Understand Abstract Classes and their implementation in Visual Basic.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.