Interface in VB.NET

In this article we will discuss about the Interface functionality.
  • 1935
Download Files:
 

Interface:- An interface is a type that defines a programming contract without any implementation at all, means that an interface defines the calling signature for a set of methods and properties ,and that it never contains any form of implementation.There are three important aspect to working
with Interface

  • Someone must define Interface.
  • One or more programmers must create classes that implement this interface.
  • Some client side code must be written against the interface type

    iNTERFACE.gif
     

Defining an Interface:-To create an Interface definition, you use the Interface construct.
inside the interface construct, you can declare the signature for methods and properties.

Public Interface Test
    'creating an Interface named Test
    Sub display()
    Function Div() As Double
    'specifying two methods in an interface

End
Interface

Note:- All members of an Interface type are implicitly public and abstract. to this end ,it is illegal to use the Public keyword or Must Override keyword on a member inside and interface definition

Implementing an Interface:- yours first step towards implementing an Interface is to use the Implement keyword
with a class defination 

Public Class One Implements Test
    'implementing interface in class One
 
    Public i As Double = 12
    Public j As Double = 12.17
    Sub display() Implements Test.display
        'implementing the method specified in interface
 
   Console.WriteLine("sum of i+j is" & i + j)
    End
Sub
 
    Public Function Div() As Double Implements Test.Multiply
        'implementing the method specified in interface
        Console.WriteLine("Div is" & i / j)  
    End Function
 
End
Class

Note:- You can Implement multiple interfaces by adding comma-delimited list of interface name after the implement keywords. such as Public Class One :Employee,Student,College, etc.

Coding for Interface:-

Module Module1 
    Sub Main()
        Dim OneObj As New One()
        Dim TwoObj As New Two()
        'creating objects of class One and Two
        OneObj.display()
        OneObj.Div()
        TwoObj.display()
        TwoObj.Div()
        'accessing the methods from classes as specified in the interface
    End Sub

End
Module
Public
Interface Test
    'creating an Interface named Test
    Sub display()
    Function Div() As Double
    'specifying two methods in an interface

End
Interface
Public
Class One
    Implements Test
    'implementing interface in class One
 
    Public i As Double = 35
    Public j As Double = 12.17
    Sub display() Implements Test.display
        'implementing the method specified in interface
        Console.WriteLine("sum of i+j is" & i + j) 
    End Sub
 
    Public Function Div() As Double Implements Test.Div
        'implementing the method specified in interface
        Console.WriteLine("Div is " & i / j) 
    End Function
 
End
Class  
Public
Class Two
    Implements Test
    'implementing the interface in class Two
 
    Public a As Double = 56
    Public b As Double = 32.17 
    Sub display() Implements Test.display
        Console.WriteLine("Welcome to Interfaces"
    End Sub
 
    Public Function Div() As Double Implements Test.Div
        Console.WriteLine("Div is " & a / b) 
    End Function
 
End
Class   

Output:- 

Interface-output.gif

© 2013 dotNetheaven. All rights reserved.