|
-
Dec 3rd, 2002, 04:14 AM
#1
Thread Starter
Registered User
Interfaces
Forgive my ignorance.... this is a rather embarrassing question.....
I started out with VB6 a couple of years ago, just because I thought it was fun to make my own programs..... I then started support the program where I work, so I started to use it more frequently. The last few months I turned to .NET and all its object orientated "beauty". So fully object orientated programming is quite new to me.
I can say that I fully understand the Polymorphic advantages with inheritance and its concept BUT .......
I can't seem to grasp the concept of Interfaces 100%. How do they work? What are the polymorphic advantaged? Why and when would I create my own interface?
I've read about it in 2 books, but I still can't really get there, so please, do not redirect me to a page filled with technical language, try to explain it as simple as possible........
Thanks in advance
-
Dec 3rd, 2002, 06:10 AM
#2
Member
My understanding
Use interfaces when you have a lot of classes and them perform the same actions (ex: read and save), but the implementation of this actions differ form one to another.
If the implementation is similar use abstract classes.
-
Dec 3rd, 2002, 06:15 AM
#3
Thread Starter
Registered User
Thanks for the reply.... I think I still need it explained more thoroughly ....
-
Dec 3rd, 2002, 10:47 AM
#4
An interface is simply a 'set of rules' that class must follow. In other words you declare the names and parameters for certain functions in your interface, but you don't write any code. When another class implements that interface, the class must as a minimum, add thos exact same functions to its class and code them to do something.
Why is this useful you may ask? Well lets take a plug-in archetecture for your application for example. say you want to have plug-ins and your application expects a function called GetData(). How do you ensure that the plug-in has a function called GetData(). Well you can tell plug-in writers that they must impelement your interface and then trough some coding, you can determine wether the plug-in uses your interface and if it doesnt, dont load it.
Look at COM component's as another example. To make an VB6 dll run in MTS, you have to implement the ObjectContext interface becasue it contains some events that MTS will try to fire off in your dll.
In simplist terms, an interface is set of rules that a class must follow
-
Dec 3rd, 2002, 11:12 AM
#5
Frenzied Member
That a good example Mr. Anderson
Dont gain the world and lose your soul
-
Dec 3rd, 2002, 12:53 PM
#6
Thread Starter
Registered User
Thanks Cander, that helped alot.
But the book I've read does not only talk about implementation, that I feel is what you're talking about, but also about interaction with the interface..... so I still think there is more to this matter..... isn't it?
For example: One of the reason that you're not allowed to declare fields in an interface is to ensure that classes that interact through the interface do not have access to the internal data of an object. That has to mean that you have access to the other things, Properties, Methods.....?!?
Last edited by Athley; Dec 3rd, 2002 at 01:11 PM.
-
Dec 3rd, 2002, 01:17 PM
#7
One way to help see the usefulness of Interfaces is to look at how the framework uses them. Lets take IEnumerable and IEnumerator these interfaces are the contract that says your object can be used in a for..each loop. So when your code is compiled or executed, before the for..each the framework can check and say is this object allowed to be accessed in this way. SO it can check the object for the interface and see yes it can or no it can't. The same goes for some other common feaures like the Clone method, which is part of the ICloneable interface. A new one I like with .NEt is that youcan now bind to objects directly not just data (in things like a datagrid or combobox). BUT in order to bind to them then need to obey the contract for it, which is the IList interface or one of its derived versions.
Since the object is known to have a certain set of methods and properties its kind of like a subset object, because all of these different object types can be converted to that interface and used for that purpose.
-
Dec 3rd, 2002, 01:27 PM
#8
Thread Starter
Registered User
Thanks alot..... now I'm close.... (waiting for that %"!#!$ coin to fall through).... ...... don't let this stop you though, if you have more to tell that can help please share.
-
Dec 3rd, 2002, 01:44 PM
#9
The main thing is just a structural implemention. Although to extend Cander's plugin example, you can also pass different object types into the same function that accepts a common interface as the parameter.
VB Code:
Public Interface ICommon
Property Name() As String
Function getID() As Integer
End Interface
'in a class
Public Function Example(i as ICommon) As String
Return i.Name & i.getID()
End Function
'say that these 2 objects implement ICommon
Dim Ex1 As New Example1
Dim Ex2 As New SomethingElse
'even though they are 2 different types they can be passed to the function because they implement the interface
Msgbox(Me.Example(Ex1))
Msgbox(Me.Example(Ex2))
Really to understand them fully you'll just need to try them out a bit.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|