Understanding interfaces

vis781

Well-known member
Joined
Aug 30, 2005
Messages
2,016
Location
Cambridge, UK
Programming Experience
5-10
Hi everyone, and thanks for taking the time to read this..

I need some help as to why interfaces are important in vb.net. I understand that they expose certain subs or functions but leave it up to the class to implement these methods. My question is why?! If they serve no purpose other than to expose these empty methods then why not just write them in code as they are needed.

Any help in explaining how i have totally mis understood the importance of interfaces would be greatly appreciated.:)
 
vis - Let me see if I can explain it. I'll give you a realworld setup that we use here.

We build custom software for various companies. Inevitably, they have a file that they want to import into the system. In order to be able to create the import object, we need a reference to it. But we don't want to send a copy of all of our imports to all of our customers. Some of it is protected proprietary info.

So we create an interface called IImport. And we define all of the public subs, properties, and functions needed to do an import.

Then in each individual import we Implement IImport and fill in the code. Each import is different and unique. But because they are all implementing the same interface, and aadhering to the IImport contract, we can do this in the import screen in our system:

VB.NET:
Dim objImport as IImport

objImport = CreateObject(strImportPRogID) 'We get this value from the DB
objImport.Import (strFileName)

We get all the benefits of strong typing and early binding, and a guarantee that the import object created has all the necessary interfaces necessary to perform the job.

-tg
 
Ok makes a bit more sense and if i've understood correctly.....

1. You don't necessarily have to implement a interface but can instantiate one and use the methods individually?
2. They provide multiple classes with the ability to use generic methods, but they can be used differently within each separate class?
3. The main benefits are, as you have said, early binding and strong typing?

P.s I'll be quite happy if i've atleast got one of those right, well except the last one as you kind of gave that one to me!!!;)
 
1 - no... more in a sec....
2 - basicaly, yes. Try looking up "polymorphism" The methods are not generic, as each class that does the implementation will have to provide the code, but the public signature will be EXACTLY the same between all classes that implement the interface.
3 - That's part of the beni, but not all.

Ok, now for #1. An interface is empty. It must be implemented by another class. If you don't implement it, you've got nothing to instanciate. The interface is a contract between two components. Basicaly one object says to the the other (I know it sounds like the begining of a joke doesn't it?) "Hey look, I agree to hold myself to the following interface. Rest assured that every PUBLIC method, property and function in the interface will exist in my object. I may not actualy have any code behind it, but the capability to call it will exist and you may do so w/o fear of an error."

Meanwhile the other object says to the first "You have agreed that you will have the following public methods, properties and functions. Rest assured that I will hold you to that standard, and will balk at the first sign that you haven't kept up your end of the bargan."

Let's see if I can pull this off.

Let's use a car analogy. Every car has an engine, but each manufacturer will build their engine differently. Infact, even within the same make, engines will be different - Think Chevy Nova vs a Chevy Truck.

So, if we were objectifying the car, we could have an interface that describes the methods and properties of the engine. We'll call it IEngine.
So in the EngineInterface class we have this:
VB.NET:
Public Class EngineInterface
    Interface IEngine
        Property EngineSize() As Integer
        Sub ApplyBrakes()
        Sub Accelerate()

    End Interface

End Class
Now we have our interfacve defined. There's no way to write code in there. It's just a shell.

If we then create a new class, ChevyNova and tell it to implement EngineInterface.IEngine... we get this:
VB.NET:
Public Class ChevyNova
    Implements EngineInterface.IEngine

    Public Sub Accelerate() Implements EngineInterface.IEngine.Accelerate

    End Sub

    Public Sub ApplyBrakes() Implements EngineInterface.IEngine.ApplyBrakes

    End Sub

    Public Property EngineSize() As Integer Implements EngineInterface.IEngine.EngineSize
        Get

        End Get
        Set(ByVal Value As Integer)

        End Set
    End Property
End Class
Now, here is where we write the code the dictates how a Chevy Nova would apply brakes, or accelerate, or what the engine size is.

Now, do the same for Lamborgini class:
VB.NET:
Public Class Lamborgini
    Implements EngineInterface.IEngine

    Public Sub Accelerate() Implements EngineInterface.IEngine.Accelerate

    End Sub

    Public Sub ApplyBrakes() Implements EngineInterface.IEngine.ApplyBrakes

    End Sub

    Public Property EngineSize() As Integer Implements EngineInterface.IEngine.EngineSize
        Get

        End Get
        Set(ByVal Value As Integer)

        End Set
    End Property
End Class

To use them, you simply Dim a variable as type EngineInterface.IEngine. Then you can use the .NET equivelent of CreateObject (the precise code escapes me at the moment) to set it to an instance of either the Nova or the Lamborgini class.

Is any of this helping?
-tg
 
I asked same question a few days ago. I wasn't really convinced about their usage, so I asked if any could tell me what do they do for being so important? I got an answer that really convinced my self. I'll give you the same example I got when I asked "Found interfaces useful?". Datasource property of a datagrid, accepts any object wich implements IList or IListSource interface. So if you create a personal class wich implements in a good way the IList or IListSource interfaces, you can populate a datagrid with it. And that gives you a lot of possibilities to think about!. DataSet and Datatable are accepted by this property because they implement IList or IListSource interface. The main advantage of using interfaces is that they set you free of limitations about a particular type of an object. So, If you would have a sub wich accepts an interface param, then you would pass to this sub any object wich implements that interface.
Regards.
 
Ok, i think something may have just clicked!, So would i be right in saying that..

1. Even though multiple inheritance is not possible in vb.net within a single class, the use of interfaces, in a round 'a' bout kind of way provide us with the abiltiy to do that?.
2. In addition because, it seems from your example TG, that the methods and properties are public, so therefore interfaces will give an app more stability because the classes that implement them will be using, as you say, a set contract.
3. Lastly you can declare a single object that could be either one of the classes and because the interface uses a 'generic' method the object could be instantiated on any class that implements the interface so therefore two objects could be instantiated and then compared or sorted, for example?!


And thanks for the hint on 'polymorphism' I'll look it up.

Am i getting it !?!!
 
You got it.

And a lot faster than it was for me to pick up I might add. It took me several months to understand how it all works.

And to carry on about multiple interfaces... we've got objects that implement 2-3 interfaces whithin a single class. But when they are instanciated, depending on the object type (when it is dimmed) we will only get the one set of interface.

-tg
 
I wouldn't go so far as to say that i completely understand interfaces, but thanks to yourself and mariano_donati, atleast now i understand why they are used.

Thanks again, regards vis781:)
 
Back
Top