Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Implementing an Interface

Status
Not open for further replies.

NWChowd

Programmer
May 26, 2002
84
US
Novice question: Can someone explain the concept of implementating an interface? I have seen plenty of code examples, and I understand the code, but I just don't quite understand the value of implementing an interface. Can someone give me a real world example?

Thanks,
DMill

======================================
"I wish that I may never think the smiles of the great and powerful a sufficient inducement to turn aside from the straight path of honesty and the convictions of my own mind."
-David Ricardo, classical economist
======================================
 
here is a code sample of an interface i had to develop (in order to make a .NET remoting service, that absolutely needs an interface)
hope this will help you :

INTERFACE :

Code:
Public Interface IAPICoreManager
    Function GetCustomerInfo(ByVal strName As String) As APILibrary.Customer
End Interface

here i publish a method into my interface
then i create a class that implements this interface.
I will get an error until this class implements a new version of the GetCustomerInfo method (that(s the interface principles)

Code:
Class APICoreManager
    Implements APILibrary.IAPICoreManager
    Public Function GetCustomerInfo(ByVal strName As String) As APILibrary.Customer Implements IAPICoreManager.GetCustomerInfo
        'do something
    End Function
End Class

so here is a real vb.net sample
hope this helps
regards,
Elise
(author of french .NET tutorial like
 
Hi,

I have no sample to give you here, but I'll try to explain why to use an interface.

An interface is useful when you want to design patterns of functionalities (methods) shared by different classes.

The advantage of the interface is that you don't need to know what class you're dealing with. If you know that you face an object that implements the interface, you can call the interface's methods directly on it.

Interface IPrintable
Sub Print
Sub PrintPreview
End Interface

Class Invoice
Implements IPrintable
...
End Class

Class SalesChart
Implements IPrintable
...
End Class

Class mdiMain
...

Sub PrintDocument(ByVal obj As IPrintable)
'this code will work on both an Invoice or SalesChart
'object passed as argument (the sub ignores what type
'is that object, but prints it anyway)
obj.Print
End Sub

End Class

... So the main benefit is to pass objects as procedure arguments, not as their respective 'true' type, nor as the lowest-level 'Object' type, but through the mask of the interface they all implement, as this gives you access to the functionalities that impersonate the interface.
You might want to test for the object's intrinsic type (horrible select case statements), but first this is poor code, and second, it is not scaleable. You might include a 'BuildingMap' class in a future release, that also might need to be printed. If so, coding it IPrintable will allow you to let your mdiMain code unchanged, while you'll be able to print a new type of document that did not exist in the first release.

An interface summarizes a set of functionalities that several classes may have in common. That capability should be described by an adjective ending with '-able'.

Little tip : try to design a class and make it ICloneable.

Cloneability is often useful, and it is easier to understand the benefits of implementing an existing .net framework interface rather to imagine a useful interface from scratch : theory is not very helpful as interfaces are created in response to very practical needs. There is quite no need for defining interfaces unless you design complete object models. A windows forms app that just displays data records does not need interfaces, they serve mainly in objects interactions.

Hope this helps (???)

Happy New Year anyway

Grunt
 
Thanks to you both. I think I am starting to get Interfaces now.

Cheers! ======================================
"I wish that I may never think the smiles of the great and powerful a sufficient inducement to turn aside from the straight path of honesty and the convictions of my own mind."
-David Ricardo, classical economist
======================================
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top