VB.NET tutorial-Abstract Class

Object-Oriented Programming (OOP)

Abstract Class

An abstract class is created for the purpose of being inherited. We cannot create objects from the abstract class. You must add the keyword MustInherit before the class AbsAnimal to make it abstract. MustOveride keywords are used to make methods abstract. Methods in derived class must use Overrides keywords to override the methods of its abstract base class.

Module Module1
    Sub Main()
        Dim d As New Dog
        d.setnumberOflegs(4) 'setnumberOflegs was inherited from the Animal class
        Console.WriteLine("Dog Name: " & d.getdogName())
        Console.WriteLine("Number of legs: " & d.getnumberOflegs())
        'getnumberOflegs was inherited from the Animal class
    End Sub
    Class Animal : Inherits AbsAnimal 'inherit abstract class
        Private numberOflegs As Integer
        Public Overrides Sub setnumberOflegs(ByVal n As Integer) ' override abstract method
            numberOflegs = n
        End Sub
        Public Overrides Function getnumberOflegs() As Integer ' override abstract method
            getnumberOflegs = numberOflegs
        End Function
    End Class
    Class Dog : Inherits Animal
        Private dogName As String
        Public Sub setdogName(ByVal name As String)
            dogName = name
        End Sub
        Public Function getdogName() As String
            getdogName = dogName
        End Function
    End Class
    MustInherit Class AbsAnimal
        Public MustOverride Sub setnumberOflegs(ByVal n As Integer)
        Public MustOverride Function getnumberOflegs() As Integer
    End Class
End Module

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.