I.T. Discussion Community!
-Collapse +Expand
Search VB.Net Group:

-Collapse +Expand VB.Net To/From
-Collapse +Expand VB.Net Store

Prestwood eMagazine

April Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingVB.NetOOP   Print This     
  From the October 2015 Issue of Prestwood eMag
VB.Net Interfaces (Interface, Implements)
Posted 11 years ago on 10/24/2008 and updated 2/21/2009
VB.Net Code Snippet:
 A flashcard from our VB.Net Flashcards Library
 A code snippet from our VB.Net Code Snippets Page


General Info: Interface

An element of coding where you define a common set of properties and methods for use with the design of two or more classes.

Both interfaces and abstract classes are types of abstraction. With interfaces, like abstract classes, you cannot provide any implementation. However, unlike abstract classes, interfaces are not based on inheritance. You can apply an Interface to any class in your class tree. In a real sense, interfaces are a technique for designing horizontally in a class hierarchy (as opposed to inheritance where you design vertically). Using interfaces in your class design allows your system to evolve without breaking existing code.

VB.Net Interfaces

With VB.Net you define an interface with the Interface keyword and use it in a class with the Implements keyword. In the resulting class, you implement each property and method and add Implements Interface.Object to each as in:

Sub Speak(ByVal pSentence As String) Implements IHuman.Speak
End Sub
Syntax Example:
Public Interface IHuman
'Specify interface methods and properties here.
End Interface

Public Class Cyborg
Inherits System.Object
End Class

Public Class CyborgHuman
Inherits Cyborg
Implements IHuman
'Implement interface methods and properties here.
End Class

VB.Net 2008 Working WinForms Example

The following example demonstrates implementing a very simple interface. The interface is named IHuman which includes one property and one method. Our resulting class is named CyborgHuman and, for clarity, our CyborgHuman class also inherits from a class called Cyborg.

Create a form and place a button on it and alter the code as follows:

Public Class Form1
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim MyRobot As New CyborgHuman
    MyRobot.HumanName = "Nicole"
    MyRobot.Speak("Hi, my name is " & MyRobot.HumanName & ".")

  End Sub
End Class

'Notice you do not specify visibility in an interface.
'Visibility is determined by the implementing class.

Public Interface IHuman
  Property HumanName()
  Sub Speak(ByVal pSentence As String)
End Interface

Public Class Cyborg
  Inherits System.Object
End Class
Public Class CyborgHuman
  Inherits Cyborg
  Implements IHuman
  Private FHumanName As String

  Public Property HumanName() Implements IHuman.HumanName
      Return FHumanName
    End Get
    Set(ByVal value)
      FHumanName = value
    End Set
  End Property
  Sub Speak(ByVal pSentence As String) Implements IHuman.Speak
  End Sub

End Class

Default Interface Visibility: Friend

If you do not specify an interfaces visibility, the default is Friend (accessible from types in the same assembly) but an interface's members are always public -- which makes sense but is noteworthy.

Interface ITalk //Default visibility is Internal.
End Interface

More Info

Definition:  Interface


Share a thought or comment...
Write a Comment...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.

Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1207A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101443 Counter
Since 10/24/2008
   Contact Us!
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

2,903 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...

©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech