Abstract class and Interface in VB.NET

Abstract class: - It is an incomplete class whose object cannot be created. It contains the skeleton of the method, which is implemented by derived class. The abstract class is prefixed by the keyword MustInherit. The methods with in the class are prefixed by MustOverride keyword. The derived class procedure, which implements the abstract class, is prefixed by Overrides keyword.

Program

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New raj1
        a.ravi("Rahul")
    End Sub
    Public MustInherit Class raj
        Public MustOverride Sub ravi(ByVal s As String)
    End Class
    Public Class raj1
        Inherits raj
        Public Overrides Sub ravi(ByVal s As String)
            MsgBox("Hello:" & s)
        End Sub
    End Class
End Class

Interface: - It is a specification of class members not the implementation of members.

It is the substitute of multiple inheritance more than one interface can be implemented by a single class. The members with in the interface are not defined. It is public by default. Variables cannot be declared within the interface. Both sub procedure and function procedure can be stored in the interface.

Program

Public Class Form2
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As New raj1
        a.sagar(25)
        a.amit("Abhijeet")
        MsgBox(a.rahul(10, 20))
    End Sub
    Public Interface raj
        Sub sagar(ByVal x As Integer)
        Sub amit(ByVal s As String)
        Function rahul(ByVal x As Integer, ByVal y As Integer)
    End Interface
    Public Class raj1
        Implements raj
        Public Sub amit(ByVal s As String) Implements raj.amit
            MsgBox("Hello:" & s)
        End Sub
        Public Function rahul(ByVal x As Integer, ByVal y As Integer) As Object Implements raj.rahul
            Dim z As Integer
            z = x + y
            Return z
        End Function

        Public Sub sagar(ByVal x As Integer) Implements raj.sagar
            MsgBox(x)
        End Sub

    End Class
End Class
Thanks for reading


Similar Articles