I am not quite clear as to why or when to use Interfaces. Can someone post a complete, simple and small example of an Interface using VB.NET in a Console Application. How is it extensible?
-
2possible duplicate of http://stackoverflow.com/questions/1983962/how-do-interfaces-work-and-how-to-use-them-in-practical-programming – John Saunders May 05 '10 at 00:34
-
Or of http://stackoverflow.com/questions/122883/interfaces-why-cant-i-seem-to-grasp-them – Walter May 05 '10 at 12:24
-
I'd suggest reading Juval Lowy's "Programming Components in .NET"; it's a solid book for explaining lots of different important concepts, and interfaces are well covered. – STW May 05 '10 at 19:11
2 Answers
In short: Favor Composition over Inheritance
Interfaces are simply a common set of member definitions that you want one or more classes to support. The key is that you have to provide the functionality explicitly when you implement an interface.
You can achieve similar results using inheritance since two sub-classes can inherit fully-functional members from the base. But the downside of inheritance is that your sub-classes end up having a hard dependency on the base class.
Consider the following classes:
Public Class Car
Publc Sub OpenDoor(ByVal key As MyKey)
Console.WriteLine("Access granted to car.")
End Sub
End Class
Public Class House
Public Sub OpenDoor(ByVal key as MyKey)
Console.WriteLine("Access granted to house.")
End Sub
End Class
You could say that these two classes are somewhat related because they both have an OpenDoor() method. You might be tempted to even create a base class to extract common functionality.
Public Class OpenableProperty
Public Sub OpenDoor(ByVal key As MyKey)
Console.WriteLine("Access granted to property.")
End Sub
End Class
Public Class Car
Inherits OpenableProperty
End Class
Public Class House
Inherits OpenableProperty
End Class
You could then use this abstraction like this:
Public Class SecurityService
Public Sub InspectProperty(ByVal item As OpenableProperty)
Dim key As New MyKey()
Console.WriteLine("Inspecting property...")
item.OpenDoor(key)
End Sub
End Class
However, relating a house to a car based solely on the fact that you can access them with a key is a pretty weak abstraction. Heck, even a can of beans can be openable!
But there are other points where relation might occur as well. For example, both a car and a house might have air conditioning:
Public Class Car
Inherits OpenableProperty
Public Sub TurnOnAirConditioning()
Console.WriteLine("Cool breezes flowing in car!")
End Sub
End Class
Public Class House
Inherits OpenableProperty
Public Sub TurnOnAirConditioning()
Console.WriteLine("Cool breezes flowing in house!")
End Sub
End Class
Should TurnOnAirConditioning() be extracted to the base class too? What does it have to do with being an OpenableProperty? Could a JewelrySafe class inherit from OpenableProperty without an AC? The better answer in this situation is to extract Interfaces and use these to compose the functionality in our classes rather than inherit:
Public Interface IOpenable
Sub OpenDoor(ByVal key As MyKey)
End Interface
Public Interface ICoolable
Sub TurnOnAirConditioning()
End Interface
Public Class Car
Implements IOpenable, ICoolable
Public Sub OpenDoor(ByVal key as MyKey) Implements IOpenable.OpenDoor()
Console.WriteLine("Access granted to car.")
End Sub
Public Sub TurnOnAirConditioning() Implements ICoolable.TurnOnAirConditioning()
Console.WriteLine("Cool breezes flowing in car!")
End Sub
End Class
Public Class House
Implements IOpenable, ICoolable
Public Sub OpenDoor(ByVal key as MyKey) Implements IOpenable.OpenDoor()
Console.WriteLine("Access granted to house.")
End Sub
Public Sub TurnOnAirConditioning() Implements ICoolable.TurnOnAirConditioning()
Console.WriteLine("Cool breezes flowing in house!")
End Sub
End Class
Public Class JewelrySafe
Implements IOpenable
Public Sub OpenDoor(ByVal key as MyKey) Implements IOpenable.OpenDoor()
Console.WriteLine("Access granted to jewelry safe.")
End Sub
End Class
Then your abstractions can be consumed as such:
Public Class SecurityService
Public Sub InspectProperty(ByVal item As IOpenable)
Dim key As New MyKey()
Console.WriteLine("Inspecting property...")
item.OpenDoor(key)
End Sub
End Class
Public Class ThermostatService
Public Sub TestAirConditioning(ByVal item as ICoolable)
Console.WriteLine("Testing Air Conditioning...")
item.TurnOnAirConditioning()
End Sub
End Class
The SecurityService could then be used to inspect the Car, House, and JewelrySafe, while the ThermostatService could be used only to test the AC of the Car and House.
Sub Main()
Dim securityService As New SecurityService()
Dim thermostatService As New ThermostatService()
Dim house As New House()
Dim car As New Car()
Dim jewelrySafe As New JewelrySafe()
With securityService
.InspectProperty(house)
.InspectProperty(car)
.InspectProperty(jewelrySafe)
End With
With thermostatService
.TestAirConditioning(house)
.TestAirConditioning(car)
End With
End Sub
Which should produce the following results:
Inspecting property...
Access granted to house.
Inspecting property...
Access granted to car.
Inspecting property...
Access granted to jewelry safe.
Testing Air Conditioning...
Cool breezes flowing in house!
Testing Air Conditioning...
Cool breezes flowing in car!

- 996
- 9
- 25
-
A *very* nice, easy-to-understand, and quite sensible example. Kudos! Are you a teacher or somesuch? :) – pepoluan Jun 03 '11 at 01:20
-
Nope, just someone else in the trenches occasionally lending a hand. I think I was reading some Head First books at the time I wrote this and some of their style rubbed off! – Technobabble Jun 17 '11 at 21:50
-
Excellent explanation of the use of implementation vs inheritance. One comment, remove the () at the end of the explicit implements. For example Implements IOpenable.OpenDoor() should read Implements IOpenable.OpenDoor – Jim Wooley Jul 26 '11 at 02:07
-
Great explanation. New variation on the spelling of jewellery, jewelery, jewelry though. – Nick Wallace Oct 03 '13 at 02:50
-
I've always said programmers don't get penalized for spelling as long as it's consistent. Fixed now. Thanks for the head's up! – Technobabble Oct 22 '13 at 19:59
Consider this simple interface:
Public Interface IWeightedValue
Public ReadOnly Property Weight As Double
Public ReadOnly Property Value As Double
End Interface
Without even writing any more code, I can start dealing with this concept in other parts of my code. For instance:
Public Function GetWeightedAverage(ByVal points As IEnumerable(Of IWeightedValue)) As Double
Dim totalWeight As Double = 0.0
Dim totalWeightedValue As Double = 0.0
For Each point As IWeightedValue in points
totalWeight += point.Weight
totalWeightedValue += (point.Weight * point.Value)
Next
Return totalWeightedValue / totalWeight
End Function
And voila -- now for any class I write, if I just make it implement IWeightedValue
then I can calculate the weighted average for a collection of instances of this class.

- 125,917
- 54
- 300
- 447
-
I feel like I almost understand the purpose of interfaces, but it still hasn't quite clicked for me yet. Maybe I should come back and read this again in a couple of days. I'll +1 this if you give me another example or elaborate in some way. – Ross Brasseaux Sep 15 '13 at 02:32