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
            Return _id
        End Get
        Set(ByVal value As Integer)
            _id = value
        End Set
    End Property

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

    Public Property LastName() As String Implements IPerson.LastName
            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 (21 votes, average: 4.43 out of 5)
Loading ... Loading ...

29 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?


  • 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?

  • 11. Ankit  |  September 24th, 2008 at 12:49 am

    You give a great focus on Interface. But i would like to say that if you give any real time example of Use of Interface which demostrate in which situation this interface very helpful to us then it will be great. I want to say give one demo application in which Progammer can understand the need of Interface

  • 12. behzad_mn  |  September 28th, 2008 at 5:10 am


    I just dont undrestand why we need interface?

    how it helps us?!

    tank you for your article.

  • 13. Faisal Zaman  |  November 5th, 2008 at 3:35 am

    I am not understaning about very well because you should take a reall examale in this topic.

  • 14. piyush  |  December 8th, 2008 at 3:19 am

    i am searching for a project in vb.net and i want full information regarding about vb.net and its related topics

  • 15. srivani  |  February 4th, 2009 at 3:13 am

    I havae got just abasic idea about INTERFACES
    but I have not under stand what r the advantages of Interfaces for technical or flexible or developer convinense or any other

    ANY WAYS THank u very much for providing this

  • 16. Will Cooper  |  March 26th, 2009 at 2:13 pm

    This is a great article. I use interfaces but often have trouble trying to explain to others there benefit around structure and why use them at all. Many feel thye just add a layer of complexity without much benefit. If you don’t mind I’ll be using this article as it is pitched at the right level.
    Many thanks

  • 17. David  |  April 17th, 2009 at 10:16 am

    Can you please describe how using an interface would help with a circular dependancy problem in VS?

    assembly 1 references assembly 2
    assembly 1 references assembly 3
    assembly 3 references assembly 2

    I have read that i could use an interface to remove the problem between assembly 2 and 3. This is not a design issue as both 3 and 2 are class libraries which have to be seperated.


  • 18. coby  |  May 13th, 2009 at 11:49 am

    When you talk about interfaces you must remember that inheritance and implementation are two different things.
    Interfaces are implemented where classes are inherited.

    Think of an interface as a skeleton for the class implementing it.

    say for instance IAnimal the interface will determine that the animal MUST have legs, body, face ect. so every class that implements the interface MUST have the property for each of the implemented interface properties defined inside the Interface. so if you have a class dog and you implement the IAnimal interface, dog MUST have a property for Legs AND Body AND Face.
    When you INHERIT from a class ANIMAL and you specify properties in the class for Legs, Body and face. You don’t have to create the properties in the dog class because they already exist in the class inherited from.
    The interface determines that it MUST have the values to be classified as an animal where Inheritance already provides properties for the animal.

    When you INHERIT from a class ANIMAL you dont have to add properties in the class for Legs, Body and face because they already exist in the class inhereted from.

  • 19. confused  |  May 30th, 2009 at 7:19 am

    What I don’t understand is implementing the ICOMPARABLE of .NET. Why do we need to use CompareTo method? Why can’t we create our own method to compare values? If there is no code in interfaces, what’s the use?

  • 20. mukund  |  June 2nd, 2009 at 3:31 am

    I am confusing to use interfaces.I want to know exact use of interfaces.My problem is:
    W e can directly write methods in class instead of using interface.Then what is need of interface.

    If anyone knows pls email me:


  • 21. Mohsin  |  June 15th, 2009 at 5:26 am

    Well, all that was good, helpfull stuff about interfaces.
    Got so much out of it, but still not feeling confortable saying that i have understood interfaces completely.

  • 22. AnonNoob  |  September 7th, 2009 at 12:34 pm

    I think the whole point of interfaces is to implement ‘good code practise’, nothing more.

    You use it to bind objects using an interface to a class, so you know 10 years later, which properties you have to construct or use. It is just a contractual aspect for coding.

    That’s my take on it anyway.

    You could be lazy (like me) and just use class properties and public / private methods to expose the parts you want other parts of the projects you want access to.

  • 23. Lakshmi  |  October 27th, 2009 at 12:04 am

    can u explain why we need to go for interfaces rather than calling a method.. Let me know whats the difference…

  • 24. Suresh  |  October 31st, 2009 at 1:22 pm

    Very Nice Article on Interfaces!

    Can you provide me an real time example of when and where to use interfaces.

  • 25. pratap  |  November 11th, 2009 at 6:14 am

    what is the benefits of using interface?

  • 26. Timan  |  December 1st, 2009 at 1:54 pm

    What is the real time use of Interface???

  • 27. Alexandre  |  February 16th, 2010 at 7:52 am

    I am very gladed to see such code of interface in vb dot net

  • 28. Adel  |  February 22nd, 2010 at 7:00 pm

    My understanding is that in .NET technology ,you can not inherit multiple classes .However, it is possible in VC++
    e.g. in .NET a class has one and only one parent(derived Class)
    furthermore, it could implement more than one interface except that it takes only the declaration parts not the implementation.
    In fact, it is meaningless if some class has more than one father
    it only inherits its father’s wealth(Properties and methods) and spends (implements) it.
    On other hand,it can not inherits other parties’ wealth but it may
    spend (implement) it.

  • 29. Tjaart  |  March 4th, 2010 at 4:44 am

    For people who dont understand interfaces:

    Interfaces make your code pluggable because you know when something implements a certain interface that it has certain functions or properties.

    IEnumerable for example lets you know that you have an enumarable indexable list of items. Say you want to write a function that can take arrays or lists. Both implement IEnumerable, so instead of writing an overloaded function to cater for both types, you simply make your function parameter an IEnumerable. Example:

    Private Function RandomizeItems(ByVal items as IEnumerable)

    instead of

    Private Function RandomizeItems(ByVal items as List(Of T))

    Private Function RandomizeItems(ByVal items as T())

    Get it?

Leave a Comment


Required, hidden

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

Trackback this post  |  Subscribe to the comments via RSS Feed

Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles


Most Recent Articles


 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities