Here, we can create a Sample1 base class and then inherit the Sample1 class into the Sample2 class and override the Sample1 class method into the Sample2 class. After that, using base class comparison, we named the base and derived class method.Program :
Below is the source code for naming the base class and the derived class overridable method using the base class reference. The program given is compiled and successfully executed.
'VB.net program to call base class and derived class
'overridable method using base class reference.
Module Module1 Class Sample1 Overridable Sub Fun() Console.WriteLine("Sample1.Fun() called") End Sub End Class Class Sample2 Inherits Sample1 Overrides Sub Fun() Console.WriteLine("Sample2.Fun() called") End Sub End Class Sub Main() Dim S As Sample1 = New Sample1() S.Fun() S = New Sample2() S.Fun() End Sub End Module
We created a Module1 module in the program above that comprises two classes, Sample1 and Sample2. We created the method Fun() in the Sample1 class, which is declared as overridable. Then the Sample1 class was inherited into the Sample2 class. We use the Overrides keyword to override the Fun() method in the Sample2 class.
Finally, we created a Main() function, which is the program entry point, here we created the Sample1 class relation and then initialized the Sample1 and Sample2 object reference and named fun() method and printed the required message on the console screen.