VB.NET Interview Questions #5VB.NET Interview Questions #6

Introduction to Interfaces in VB.NET

October 23rd, 2007

Implements IPipeline, IWorker

One problem some programmers new to VB.NET and object oriented programming in general have is understanding interfaces and how they related to classes. In this article, we’ll take an introductory look at interfaces and some of the ways you can use them in your VB.NET applications.

What is an Interface?

To put it in simple terms, an interface is a definition of methods and properties. Sometimes this is referred to as a ‘contract’. If a class agrees to implement an interface it must implement all of the properties and methods defined.

One difference you’ll see in an interface vs. a class is that there is no code, only the templates, aka contracts, for the properties and methods. You’ll also notice that there is no access level defined. It’s always considered public.

Here’s an example interface:

Public Interface IPerson

    Property ID() As Integer

    Property FirstName() As String

    Property LastName() As String

End Interface 

This means that if a class implements the IPerson interface it will have to implement these routines. Here’s an example of a class implementing this interface:

Public Class Customer    Implements IPerson

    Protected _id As Integer
    Protected _firstName As String    Protected _middleName As String
    Protected _lastName As String

    Public Property ID() As Integer Implements IPerson.ID
        Get
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

    Public Property FirstName() As String Implements IPerson.FirstName
        Get
            Return _firstName
        End Get
        Set(ByVal value As String)
            _firstName = value
        End Set
    End Property
    Public Property MiddleName() As String
        Get
            Return _middleName
        End Get
        Set(ByVal value As String)
            _middleName = value
        End Set
    End Property

    Public Property LastName() As String Implements IPerson.LastName
        Get
            Return _lastName
        End Get
        Set(ByVal value As String)
            _lastName = value
        End Set
    End PropertyEnd Class 

As you can see, each of the properties above, except for MiddleName, has an Implements statement that defines how it will fulfill the Interface contract. The rule is that you can add to the interface inside the implementing class but you can’t take away.

Remember that implementing an interface isn’t the same as creating an instance of a class or creating a subclass. All you’re doing is defining how the external interface should look, not what goes on inside. People do get confused over that point.

Coding Interfaces

Interfaces can be very simple or complex. Most current OOP thought suggests that interfaces be limited to a single method. For example, you might have an IUpdate interface that defines a single Update function and you might have another interface called ICreate that defines a single Create function. You would then combine them into classes that needed to implement them, as in this example:

Public Interface ICreate
    Function CreateNew(ByVal id As Integer) As Boolean
End Interface

Public Interface IUpdate
    Function Update(ByVal id As Integer) As Boolean
End Interface
Public Class Customer
    Implements IPerson, IUpdate, ICreate

As you can see, a class can implement multiple interfaces. Whether it’s better to design with a lot of different interfaces or to group them into logical sections is really up to your own needs. My own take is a bit of a compromise in that I keep them simple with just a few tightly related methods or properties but I don’t religiously limit each interface to a single item.

Another use for interfaces is in defining a common interface for a plug-in architecture as I mentioned in this previous article: How To Create Application Plug-ins In VB.NET. In this case, you will want to have a more complex interface design in most cases since your main app will need a stronger connection to the plug-in.

Do you have any questions about using Interfaces in VB.NET? Please feel free to leave a comment and ask about it.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials


Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (4 votes, average: 4.25 out of 5)
Loading ... Loading ...

10 Comments Add your own

  • 1. Khushal Patel  |  October 24th, 2007 at 9:54 pm

    I think you should also give the diffrences between abstract Class Vs Interfaces. it will really helps newbies to understand about creating extensible appications. :-)

  • 2. jfrankcarr  |  October 24th, 2007 at 10:07 pm

    Thanks Khushal,

    That’s a good idea. I’ll probably do an article on that at some point. I wrote this article mainly for someone who had looked at my article on creating application plug-ins and they were having trouble understanding what an Interface was.

  • 3. Sangita Patra  |  November 18th, 2007 at 10:58 pm

    can u please clear me that what is the need of interface in vb.net and the difference between a class and an interface

  • 4. MARY  |  January 21st, 2008 at 2:19 pm

    codes in visual basic for addin two numbers.

  • 5. strangers  |  January 23rd, 2008 at 2:15 pm

    Can you send me code for this:
    I am developing common user control in one project where i have to develop a code for common user control.

    So my problem how is that i am able to develop a code for the conneection of this form but i am not able to access the value.
    More Clealy i would say it may any number of form how do i navigate a records with the same user control. I have used adodb connectivity.

  • 6. Greg Frantsen  |  January 30th, 2008 at 10:59 am

    I’m still a little foggy on the “why” of interfaces. Why would someone want to use them? What do they promote/protect against? Maybe an example of something with and w/o an interface?

    Thanks.

  • 7. Carlosfocker  |  May 5th, 2008 at 11:42 am

    The biggest thing that interfaces protect you from is the dangers of multiple inheritance. I would suggest doing some research on the diamond of death issue that comes up with languages that allow multiple inheritance.

  • 8. AMol  |  June 6th, 2008 at 8:32 am

    This is a useful article.I got clear understanding of interfaces

  • 9. Rakesh  |  July 11th, 2008 at 4:59 am

    I do not understand that how Interface are subsitute of multiple inheritance in vb.net.Can we inherit System.Web.UI.Page and Syatem.Web.UI.MasterPage in a single class using Interface?

  • 10. Rakesh  |  July 14th, 2008 at 1:57 am

    I do not understand how Interface is subsitute of multiple inheritance in vb.net? Can we inherit “System.Web.UI.Page” and “System.Web.UI.MasterPage” in a single class?

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Most Popular Articles

Highest Rated Articles

Categories

Most Recent Articles

Feeds

 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities

Archives