Inheritance in VB.NET

How to use Inheritance in VB.NET?


Inheritance in 2008

Inheritance in is method by which the properties of the base classes are added to the derived classes. In the keyword Inherits is used in the derived class to specify its base class.
The MustInherit keyword is used to speciy that class can be used only as a base class,NotInheritable is used to specify that a class cannot be inherited.

Module Module1
Public Class s1
Public a As Integer = 5
Public Function val() As Integer
Return a
End Function
End Class
Public Class s2
Inherits s1
Public c As Integer = 20
Public Function add() As Integer
Return c + a
End Function
End Class
Sub Main()
Dim res As New s2
System.Console.WriteLine("Final Value is::")
End Sub
End Module

Final Value is: 25

In the above example the value of a is inherited to from base class s1 to the derived class s2. Using the instance res of class s2 the values of 'a' as well as 'c' is added to give the result.

Visual Basic Tutorial

Ask Questions

Ask Question