Question Confused by Interfaces - Is ISomething an Interface or an Object or both?

aychekay

Member
Joined
Oct 10, 2011
Messages
11
Programming Experience
3-5
I'm working on learning VB.Net after having programmed in VB6, VBA, and VBScript for several years.

I'm assuming that something that begins with the letter I is an interface. However, a lot of places I see people/documentation referring to what appears to be class objects that begin with I. I guess I'm under the impression that Interfaces are contracts that class objects can agree to.

I think part of my confusion comes from the fact that I see code examples where class objects implement a specific interface but in the example they do not appear to have any properties, events or methods that reference the interface/contract.

The reason I'm concerned about this is because I would like to learn to bind forms and controls to business objects. I understand that knowing how to use interfaces is critical to making this work.

Just wondering if anyone can shed any light on my confusion.
 
Interface is a type that only declare member definitions (also referred to as a 'contract' sometimes), but no implementations. Classes and structures are types that contain implementations, and they can implement interfaces. An instance (object) of a class Something that implements interface ISomething can be seen as both types, so a variable as type ISomething can refer to the Something object. Through this variable reference you can only access the interface members. As such it can be useful for a method to have parameters that is interface type, because many different class objects can be passed to it as long as they implement that interface. Implementing classes must always implement all members of the interface.

The 'I' prefix notation is part of naming conventions, interface types should always use it. For other kinds of types it would thus be a conflict of interests and wrong to use that as prefix in class name. I guess people could use 'i' prefix in variable names that refer to objects of a particular interface type, but it would presumably always be a lower case letter according to naming convensions, it would also be considered hungarian notation which is unnecessary and a bad practise.
 
Programming interfaces work in basically the same way as interfaces in the real world. Think about USB. It defines a set of functionality that a device must provide in order to be used in a certain way, i.e. connected to a USB port, but it doesn't say anything about what the device does other than that. Is a printer a printer or is it a USB device? The answer is that it's both. The same goes in programming. An array is both an array and an IList. because it is an IList, you are able to bind it to a ListBox, ComboBox or some other control(s) in a Windows form. The IList interface defines a set of functionality that an object must implement to be considered a "list", but it doesn't say anything about how that functionality must be implemented or what other functionality the object may implement.
 
I think the part that has confused me the most is when I see code that declares a variable As IInterface. The way interfaces are generally described there is little or no talk about this practice.
 
You're trying to complicate something that's very simple. Have you ever seen an Integer or String assigned to an Object variable? Was that confusing? Have you ever seen an object assigned to a variable of a base type, e.g. a Button assigned to a Control variable? This is no different.

The main reason for using an interface type when declaring a variable is so that you are able to assign to it an object of any type that implements that interface. One of the most common reasons for changing the type would be unit testing. For instance, a service class might invoke a repository class and that repository might retrieve data from a database. In your service class you use the repository interface rather than a concrete class. That means that you can use a real data access object in your application but in your unit tests you can use a fake or mock repository, i.e. one that implements the same interface but just returns some canned data instead of real data from a database. The service has no idea about the switch because all it cares about is the interface but using the fake or mock object allows your unit tests to be fast and independent of any ower-level functionality or data.
 
Back
Top