VB.Net program to override a base class method into derived class

Here, we are going to learn how to override a base class method into derived class in VB.Net?
Submitted by Nidhi, on December 28, 2020

Here, we will create a base class Sample1 and then inherit Sample1 class into Sample2 class and override the method of Sample1 class into Sample2 class.

Program/Source Code:

The source code to override a base class method into the derived class is given below. The given program is compiled and executed successfully.

' program to override a base class method 
'in the derived class.

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 S2 As New Sample2()
    End Sub
End Module


Sample2.Fun() called
Press any key to continue . . .


In the above program, we created a module Module1 that contains two classes Sample1 and Sample2. In the Sample1 class, we created method Fun(), which is declared as overridable. Then we inherited Sample1 class into the Sample2 class. In the Sample2 class, we override the Fun() method using the Overrides keyword.

At last, we created a Main() function, which is the entry point for the program, here we created the object of Sample2 class and called the fun() method, here fun() method of Sample2 class will be called and print the appropriate message on the console screen.

VB.Net Basic Programs »



Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.