Abstract class in VB.NET

This article defines the abstract class and also defines why we use abstract class when abstract class can not be instantiated.
  • 4871
Download Files:
 

This article defines the abstract class and also defines why we use abstract class when abstract class can not be instantiated.

Abstract Class

Abstract class is a special kind of class that cannot be instantiated. It only allows other classes to inherit from it but cannot be instantiated. These are the important point which are related to the abstract class.

  1. abstract class may contain concrete methods.
  2. class may contain non-public members.
  3. abstract class can be used as a single inheritance.
  4. abstract class can be invoked if a main() exists.

why we need a abstract class that cannot be instantiated 

It only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In other word, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

Creating a abstract class

we use MustInherit keyword to create abstract class. Abstract classes can also specify abstract members. Like abstract classes, abstract members also provide no details regarding their implementation. Only the member type, access level, required parameters and return type are specified.  and to declare the abstract member we use the MustOverride keyword.

For example

Imports System.Console

Imports System.Math

Module Module1

    Public MustInherit Class Abstractclass

        Public MustOverride Function square() As Integer

        Public MustOverride Function cube() As Integer

    End Class

    Public Class AbstractFirst

        Inherits AbstractClass

        Dim A As Integer = 4

        Dim B As Integer = 5

        Public Overrides Function square() As Integer

            Return A * A

        End Function

        Public Overrides Function cube() As Integer

            Return B * B * B

        End Function

    End Class

    Sub Main()

        Dim abs1 As New AbstractFirst()

        WriteLine("square of A is :" & " " & abs1.square())

        WriteLine("Cube of B is :" & " " & abs1.cube())

        Read()

    End Sub

End Module

 

OUTPUT 


ab1.gif

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.