ARTICLE

Interface in VB.NET

Posted by Munesh Sharma Articles | VB.NET Articles March 22, 2011
In this article we will discuss about the Interface functionality.
Download Files:
 
Reader Level:

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

 
 

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor