Abstract classes in VB.NET

In this article we will describe the functionality of abstract classes.
  • 4753
We know that all the classes can be separated in to two categories, concrete classes and abstract classes  

Concrete classes:-"Classes are concrete classes if they support object instantiations using the new operator."

Abstract classes:-"Classes are abstract classes  if they do not support object instantiations." 

Now we will discuss about the Abstract classes in VB.NET, explaining why we use the abstract classes in programming.because an abstract base class enable you to define a programming contract with the partially completed implementation and other functionality that are given bellow 

  • Defining a class as an abstract class enables you to document your intention to use the class exclusively as base class.
  • An abstract base class is like  a concrete base class in that it may contain both override and non override methods.
  • Abstract base classes are more flexible than concrete base classes because they can also contain abstract members.   

Syntax of abstract classes in VB.NET:- When you create the definition for the base class, you must add the MustInherit keyword to make it abstract

Public MustInherit Class className
    Public Overridable Function functionName() As String
        Return " That are vehicle"
    End Function
    'othere class member function omity for clarity


Note:-A class defined with the MustInherit keyword is abstract and , therefore ,can not be directly used to create an object.

coding for abstract classes in VB.NET:
Module Module1 
     Public MustInherit Class AbstractClass
         'declaring an abstract class with MustInherit keyword
        Public MustOverride Function Add() As Integer
        Public MustOverride Function Subs() As Integer
        'declaring two abstract members with MustOverride keyword
    End Class
     Public Class AbstractOne
         : Inherits AbstractClass
         'implementing the abstract class by inheriting
        Dim i As Integer = 50
         Dim j As Integer = 30
         'declaring two integers
         Public Overrides Function Add() As Integer
            Return i + j
         End Function
        'implementing the add method
         Public Overrides Function Subs() As Integer
            Return i - j
         End Function
        'implementing the subs method
     End Class
    Public Class Abstracttwo : Inherits AbstractClass
         Dim X As Integer = 34
         Dim y As Integer = 30
         Public Overrides Function Add() As Integer
            Return X + y 
         End Function
         Public Overrides Function Subs() As Integer
            Return X - y 
         End Function
    End Class
    Sub Main()
         Dim abs As New AbstractOne()
         'creating an instance of AbstractOne
        Console.WriteLine("first class Sum is" & " " & abs.Add())
         Console.WriteLine("first Substraction is " & " " & abs.Subs())
         Dim abt As New Abstracttwo()
         'creating an instance of AbstractOne
        Console.WriteLine("second class Sum is" & " " & abt.Add())
         Console.WriteLine("second class Substraction is " & " " & abt.Subs())
         'displaying output
    End Sub




© 2020 DotNetHeaven. All rights reserved.