Visual Basic Method Overriding

In visual basic, Method Overriding means override a base class method in the derived class by creating a method with the same name and signatures to perform a different task. The Method Overriding in visual basic can be achieved by using Overridable & Overrides keywords along with the inheritance principle.

 

Suppose, if we want to change the behavior of the base class method in a derived class, we need to use method overriding. The base class method which we want to override in the derived class that needs to be defined with Overridable keyword and we need to use Overrides keyword in derived class while defining the method with same name and parameters then only we can override the base class method in the derived class.

 

In visual basic, the Method Overriding is also called as run time polymorphism or late binding. Following is the code snippet of implementing a method overriding in visual basic programming language.

 

' Base Class

Public Class Users

    Public Overridable Sub GetInfo()

        Console.WriteLine("Base Class")

    End Sub

End Class

 

'Derived Class

Public Class Details

    Inherits Users

    Public Overrides Sub GetInfo()

        Console.WriteLine("Derived Class")

    End Sub

End Class

If you observe the above code snippet, we created two classes (“Users”, “Details”) and the derived class (Details) is inheriting the properties from base class (Users) and we are overriding the base class method GetInfo in derived class by creating a method with same name and parameters, this is called a method overriding in visual basic.

 

Here, we defined the GetInfo method with Overridable keyword in the base class to allow derived class to override that method using Overrides keyword.

 

As discussed, only the methods with Overridable keyword in the base class are allowed to override in the derived class using Overrides keyword.

Visual Basic Method Overriding Example

Following is the example of implementing a method overriding in visual basic programming language.

 

Module Module1

    Public Class BClass

        Public Overridable Sub GetInfo()

            Console.WriteLine("Learn C# Tutorial")

        End Sub

    End Class

    Public Class DClass

        Inherits BClass

        Public Overrides Sub GetInfo()

            Console.WriteLine("Welcome to Tutlane")

        End Sub

    End Class

    Sub Main(ByVal args As String())

        Dim d As DClass = New DClass()

        d.GetInfo()

        Dim b As BClass = New BClass()

        b.GetInfo()

        Console.WriteLine("Press Enter Key to Exit..")

        Console.ReadLine()

    End Sub

End Module

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Method Overriding Example Result

 

This is how we can achieve the method overriding in visual basic to override a base class method in child class by defining a method with the same name and signatures based on our requirements.

Visual Basic Overloading vs Overriding

Following are the difference between method overloading and method overriding in visual basic programming language.

 

TypeDescription
Method Overloading Method Overloading means defining a multiple methods with same name but with different parameters.
Method Overriding Method Overriding means override a base class method in derived class by creating a method with same name and parameters using virtual and override keywords.