Example :  Polymorphism examples in VB .NET program in Subroutine to use Function/Method Overriding using Polymorphism in Console Application.
Imports System
Module Module1

    Public Class A
        Public Overridable Sub display()
            Console.WriteLine("Base Class Message")
        End Sub
    End Class

    Public Class B
        Inherits A
        Public Overrides Sub display()
            Console.WriteLine("Derived Class Message")
        End Sub
    End Class

    Sub Main(ByVal args As String())
        Dim a As A = New A()

        Dim b As B = New B()
    End Sub
End Module

Base Class Message
Derived Class Message

NB: Method Overriding in VB .Net override a base class method in the derived class using virtual, overridable and override keywords.
Example : Polymorphism examples in VB .NET program in to display Abstract Class & Method using Polymorphism in Console Application.
Module Module1
    MustInherit Class Program1
        MustOverride Sub Display()
        MustOverride Sub Output()
    End Class

    Class Program2
        Inherits Program1

        Overrides Sub Display()
            Console.WriteLine("Override method Display() called")
        End Sub

        Overrides Sub Output()
            Console.WriteLine("Override method Output() called")
        End Sub
    End Class

    Sub Main()
        Dim Obj As New Program2()
    End Sub
End Module

Override method Display() called
Override method Output() called

NB: Here, the MustInherit keyword is used to create an abstract class. An abstract class includes a method that is also abstract. The MustOverride keyword is used to create abstract methods.



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.