"John" <Jo**@discussions.microsoft.com> wrote
What is the purpose / benefit of using an interface statement?
It doesn't seem like anything more than a different way to make a class...
(except you can't define any procedures in an interface like you can do in a
class).
I'm obviously missing something...
Interfaces are simply a contract for service. When any object declares
that they support an interface, it simply means there is a group of
properties and routines that are guaranteed to be present. The combination
of properties or routines are listed in the interface so that every object
that supports that interface has a group of properties and routines that
are all identically named, as listed in the interface declaration.
Interfaces can be implemented by completely different objects, which is what
makes them very useful. You can have a car and a house both implement a
ManufacturedItem interface that lists things like Color, Date, Time and
Place of Manufacture, Retail Cost, and so on. The beauty of this is that
you can put all such objects in a list, and get a total cost, for example,
without having to know what each individual item is. As long as they all
support the same ManufacturedItem interface, you can call into them to
get their cost value, or other such properties from the interface, without
actually knowing what type of object it is. If it supports an interface,
you can declare a variable as that interface, and assign those objects to
it. Because of the contract, it will respond to calls made on that interface.
For all practical purposes, it looks like an object that was declared as that
interface type.
Going a little deeper into OO, an interface also provides the mechanism for
giving objects a chameleon effect. That is, they can look to be like an object
of many different types by supporting many different interfaces. A document
object, for example, might represent itself as a text document, an HTML document,
a letter, a fax, a report document, a chart, a graph, or other such items by
supporting the interfaces for those different types. I don't know if there are
such interfaces for those types, but for the sake of explanation, you will have
to imagine there are such document interfaces, and that a document you create
can support many such interfaces. Those looking to find a text document will
see it as a text document, and those looking to find an HTML document will see
it as an HTML document, and so on, for all the different interfaces it supports.
Supporting many different interfaces becomes useful when you have a storehouse
of routines that can produce specific output from specific objects. For example,
because this document supports the previous list of interfaces, it can be passed
to a form that knows how to display text documents, or to a browser that knows
how to display HTML documents, or to a printer that can handle letters, or a fax
machine that send faxes, or a spreadsheet that handles reports, or to other such
devices that know what to do with charts and graphs. Because that one object
supports all these different interfaces, it can be passed without modification
to many different routines that (in this case) produce output in their own
specific domains. In OO circles, the correct term for that ability is called
polymorphism.
HTH
LFS