Using Interfaces in VB.NET

matt1356

Member
Joined
Nov 30, 2006
Messages
5
Programming Experience
3-5
Hi,

I was trying to teach myself Remoting techniques when I stumbled across the concept of Interfaces. I've made up a small console program that uses an Interface and it works fine however when I comment out the Interface code it works just the same!!! So, can someone please explain why I would want to use an Interface instead of just using classes? I'm really confused as to the practical uses of an interface...I've listed the code below:

Imports System.Console

Module Module1
Sub Main()
Dim OneObj As New One
Dim TwoObj As New Two
'creating objects of class One and Two
OneObj.disp()
OneObj.multiply()
TwoObj.disp()
TwoObj.multiply()
'accessing the methods from classes as specified in the interface
End Sub

End
Module

'Public Interface Test
'Sub disp()
'Function Multiply() As Double
'End Interface

Public Class One
'Implements Test
Public i As Double = 12
Public j As Double = 12.17
Sub disp() 'Implements Test.disp
WriteLine("sum of i+j is" & i + j)
Read()
End Sub

Public Function multiply() As Double 'Implements Test.Multiply
WriteLine(i * j)
Read()
End Function

End
Class

Public
Class Two
'Implements Test
Public a As Double = 20
Public b As Double = 32.17

Sub disp() 'Implements Test.disp
WriteLine("Welcome to Interfaces")
Read()
End Sub

Public Function multiply() As Double 'Implements Test.Multiply
WriteLine(a * b)
Read()
End Function

End
Class

Many thanks.
 
That is a shocking example of how to use an interface, i'm sure i've seen one just like it on the web. Consider this....

Public Interface IPopupInterface

Sub ShowPopup(byval Location as Point)

End Interface.

So in the framework there are a couple of different types of popupmenu, context menu, dropdown menu.

Lets say that you have a class that has popup menu capability but at design time you don't which one will be passed to it, but all you need is the ShowPopup sub that is defined in the interface. So if ContextMenu implemented IPopupInterface, and so does the dropdown menu we can now pass them as the same type to the class that needs them.

VB.NET:
Expand Collapse Copy
Public Class INeedAPopup
 
Public Sub New (byval Popup as IPopupInterface)
 
End Sub
End Class

Now when we create a new instance of the INeedAPopup Class we can pass our contextmenu class that implements our interface and we can also pass our dropdown menu class because they implement the IpopupInterface. But the only method that would be available is the ShowPopup Sub that is defined in the interface.

So do you see that interfaces can provide us a way to interact with different types but only use the methods defined in the interface.

Have i confused you even more here?
 
That example would make more sense if your Sub Main used this code:
VB.NET:
Expand Collapse Copy
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] iobj [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Test
iobj = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] One
iobj.disp()
iobj.Multiply()
iobj = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Two
iobj.disp()
iobj.Multiply()
[/SIZE]
See how the 'iobj' variable is declared as type 'Test' (= the interface), and new instances of both classes One and Two can be assigned this directly.
 
Thanks for your replies but the penny still hasn't dropped!

JohnH: I don't understand how your code represents an improvement over the original sub main. I can see that there is one line of code less but the person writing the code still needs to know that there are two classes named One and Two. I thought the idea behind an interface was that the actual implementation of the classes could be hidden....

Vis781: Well spotted, I DID get the example quoted from the web!! So, from your example the interface takes the popup box and then passes it to the class who decides what to do with it depending on it's type. Therefore we are hiding the code within the class??? Why not just create a function in a class that would evaluate the type of popup box and action accordingly. Do you have a continuation of the code example supplied that I could download?

Ta
Matt
 
They key point of interfaces is that you have 2 or more different classes, but you want them to expose some equal functionality, and you want to handle these different instances as if they were the same type. Without interface you would have to write one code for each class type, with interface you can write one code that spawns them all. Your original code didn't use the interface at all - my modification did only work with the Test type, it didn't care about the One or Two class type, only the interface mattered. Of course the programmer have to know what classes to work with, you have to read the documentation to know if an existing class implement a specific interface, when writing your own code you can choose to declare and implement interface which will make coding and readability much easier to handle.

A slightly different angle and in some cases alternative to interface is inheritance, another concept of object oriented programming, you can choose to inherit several of your classes from a base class, which means a derived class can be boxed to a base type variable, in this case you know all the base functionality will exist for every derived class and can write code that exploit this commonness the same way as with interface.

Interface works better for very different classes where you still require something in common and accept to write the code to implement the interface, inheritance works better when the class does have a real base in common and you don't need to write independent code in derived class for base functionality.
 
No, in my example the interface provides us with a way to interact with two different classes that are not descended from one another through inheritance. For example.

VB.NET:
Expand Collapse Copy
Class1
 
Public Sub DoSomething()
 
End Class
 
Class2
Inherits Class1
 
End Class


Effictively, class2 in now an identical clone of class1, BUT they are two distinctly different types. Now if i have a sub that takes class2 as a parameter..

VB.NET:
Expand Collapse Copy
Public Sub DoWork(Byval Cls as Class2)
End sub
I can pass an instance of class1 to that sub because it is decended from class2 and i can also pass class2 to that sub because that is the parameter it asks for. Now consider the same example but slightly modified...

VB.NET:
Expand Collapse Copy
Public Class Class1
 
Public Sub DoSomething
End Sub
 
End Class
 
Public Class Class2
 
Public Sub DoSomething
End Sub 
End Class

Ok, class2 no longer inherits from class1. So if we were to try to pass them to that same sub..

VB.NET:
Expand Collapse Copy
Public Sub DoWork(Byval Cls as Class2)
End Sub
We are no longer able to pass an instance of class1 to that sub because it doesn't have a link to class2 through inheritance.

Understand so far?

So now consider this...

VB.NET:
Expand Collapse Copy
Public Interface IDoSomethingInterface
 
Sub DoSomething
 
End Interface
 
Public Class Class1
Implements IDoSomethingInterface
 
End Class
 
Public Class Class2
Implements IDoSomthingInterface
 
End Class


So Class2 still doesnt inherit from class1 but we can pass either of those two classes to our sub through a reference to the interface itself..

VB.NET:
Expand Collapse Copy
Public Sub DoWork(Byval Cls as IDoSomethingInterface)


We can now pass class1 and class2 to that sub because they both implement that interface. BUT the only methods we would have available is the one defined in the interface which is ..

VB.NET:
Expand Collapse Copy
Sub DoSomething

Cna you see how this lets us relate different types through a common interface and have them passed as the same type. I know what you may be thinking, i can do this with inheritance anyway. But a single class cannot inherit from more than one other class, BUT a single class can implement mutilple interfaces. This is how we achieve polymorphism. Linking together different types with no inheritance link, but they do share a common interface.

With me?
 
Back
Top