Object-Oriented Programming (OOP)

Interface


An interface can be used as a template for other classes. Its purpose is to be inherited. It sounds like an abstract class. However, in the interface, you cannot declare variables unless they are constants. In addition, methods can be declared, but those are without bodies.

Example:
Module Module1
 
    Sub Main()
        Dim d As New Dog
        d.setnumberOflegs(4) 'setnumberOflegs was inherited from the Animal class
        d.setdogName("Aluk")
        Console.WriteLine("Dog Name: " & d.getdogName())
        Console.WriteLine("Number of legs: " & d.getnumberOflegs())
        'getnumberOflegs was inherited from the Animal class
 
        Console.ReadLine()
    End Sub
 
    Class Animal : Implements AnimalTemp 'Implement the interface
 
        Private numberOflegs As Integer
 
        Public Sub setnumberOflegs(ByVal n As Integer) Implements AnimalTemp.setnumberOflegs
 
            numberOflegs = n
        End Sub
 
        Public Function getnumberOflegs() As Integer Implements AnimalTemp.getnumberOflegs
 
            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
 
 
'Defining interface AnimalTemp
    Interface AnimalTemp
 
        Sub setnumberOflegs(ByVal n As Integer)
        Function getnumberOflegs() As Integer
 
    End Interface
  End Module


HTML Comment Box is loading comments...



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.