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()
        a.display()

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

Output:
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()
        Obj.Display()
        Obj.Output()
    End Sub
End Module

Output:
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.

Loading


0 Comments

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.