ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

VB.Net program to demonstrate the MustOverride keyword

Here, we are going to demonstrate the MustOverride keyword 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. In the Sample1 class method fun() will be declared using the MustOverride keyword.

Program/Source Code:

The source code to demonstrate the MustOverride keyword is given below. The given program is compiled and executed successfully.

'VB.net program to demonstrate the MustOverride keyword.

Module Module1
    MustInherit Class Sample1
        MustOverride Sub Fun()
    End Class

    Class Sample2
        Inherits Sample1
        'Here we need to override the Fun() method 
        'otherwise it will generate the error.
        Overrides Sub Fun()
            Console.WriteLine("Override method Fun() called")
        End Sub
    End Class
    
    Sub Main()
        Dim S As New Sample2()
        S.Fun()
    End Sub
End Module

Output:

Override method Fun() called
Press any key to continue . . .

Explanation:

In the above program, we created a module Module1 that contains two classes Sample1 and Sample2.

In the Sample1 class, we declared the Fun() method using MustOverride, if we declared a method using the MustOverride keyword then we need to declare a class using the MustInherit keyword. Then we override the Fun() method in the Sample2 class 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 that will print the appropriate message on the console screen.

VB.Net Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

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

© https://www.includehelp.com some rights reserved.