I screw the screw with a screwdriver – Interfaces

Interfacing and Implementation

Several times on the internet I’ve been in some type of discussion in which I refer to interfaces and implementation. Several times I get a return with some statement about how they aren’t using an interface at this moment, or just explicit confusion to the entire statement all together. Now this doesn’t surprise me, I assumed the person understood the word when they didn’t. It took myself a while to figure out what the hell they are, and that was with both friends and books shoving the concept in my face repeatedly until the light lit up and went ‘ding ding ding!’ This entry here is written in the hopes that it goes ‘ding’ for you as well.

The words are just vocabulary describing concepts. The words travel from 1 language to another easily. It just so happens a few languages also have data types and keywords called the same thing. The only reason these keywords are called that is because the action they perform is equivacle to the definitions of the words as a concept.

Consider it like this… why is a screw called a screw? Because you screw a screw. The name of the screw directly relates to the action performed on or by the screw. I don’t know which definition of word came first (if the noun or the verb), but either way interface is kind of the same idea. We just know that the concept of interface came before the data type interface.

WHAT ARE THEY!? Ok ok, let’s get to the most important part of this article. What the hell is an interface and implementation as a concept.

Interface

Well let’s just look at the dictionary definition of this word. It’s not like this word was invented just for programming… it comes from English. Let’s take one of the several definitions of the word that closely relates to our situations from an English perspective.

interface – a common boundary or interconnection between systems, equipment, concepts, or human beings.

The commonality between all the definitions of interface is that it refers to a boundary. And that’s what a programmatic interface is, it’s the exterior boundary of an object. More specifically it’s the parts of an object you can see and communicate with when you aren’t inside the object. I can see a human’s mouth, I can’t see their lungs. His lungs play a large role in their ability to speak, but just from looking at them, it looks as if the mouth is doing all the work. That mouth is their speech interface.

In OOP the interface is nearly the same thing. When you define a class everything inside of the class tends to be protected and wrapped inside of it. This is called encapsulation, and is the simplest example of encapsulation in OOP. Once a Class is instantiated and other objects attempt to use it, the other objects don’t CARE what goes on inside of the Class… and the object doesn’t want the other objects to care. Instead we communicate to the object through its interface… that being the properties and methods defined by the Class.

The word can also relate to other data types as well. Such as user interface, a user interface is the parts of a program as a whole that are made available to a user to alter the program. We don’t let the user into the program to play with anything, we have a definite boundary through which there are a few defined openings they can act upon. Or a graphical user interface, its like a user interface, but displayed as graphics as opposed to command entry (ui is accessed via a shell or command prompt, gui’s are accessed through a more intuitive visual display). Further more larger design patterns can have implicite interfaces. For instance, say you create a large framework… that framework can be designed with implicite boundaries. Certain data types that can be created, and others which are hidden and internal. For instance consider the DisplayList in flash, coders of AS3 don’t get access to the nitty gritty of the DisplayList like how it is rendered or anything. But we have an interface to gain access to it and use it via the DisplayObjects, Stage, and EventDispatchers made available to us. These kind of interfaces are very complex and take a lot of planning. The boundaries of them are sometimes hard to visualize.

Implementation

When you have an interface, what are we interfacing to? Awww, the implementation. Let’s again take the English definition of the word as a verb (not as a noun).

implement – verb – to put into effect according to or by means of a definite plan or procedure.

And that is exactly what implementation is in programming as well. It the WHAT the interface is allowing you to do. Remember how I described the mouth as the interface of speech for a human. And the lungs played a role in the ability. Well the lungs are part of the implementation. It’s the what that is occuring to allow voice to be created, of course there are other parts that consist in the implementation; larynx, tongue, etc.

In OOP you can see the implementation as the actual operatable code inside of a Class. So the method definition is the interface, and the code inside that method is the implementation. Again though the word isn’t just restricted to OOP and Classes. Anytime there’s an interface, there is an implementation of that interface. In the user interface, the implementation is what occurs when you access some part of the UI. Same for a GUI. Or in a framework the implementation is the entire framework’s structure.

Let’s bring this back into flash again with its DisplayList. I said that the DisplayObjects and the sort act as an interface into this structure. Well the implementation is what the structure is actually doing. Its all the internal code of the flashplayer that renders your DisplayObjects on the stage.

Data types and keywords

So what about the data type interface and the keyword implements. Both these keywords don’t exist in all languages, in more recent languages they’ve been put into use to allow a high level language to easily describe these concepts in code form.

Both of these keywords are explicit rules defined by the language to perform the concepts as defined above. An Interface is a way to describe the interface of a Class. And when a class implements an Interface you are deciding to now write the code which defines the implementation of the Inteface. Yep that’s a lot of reuse of the same word to mean different things… and that’s why it is confusing. But it’s just like the ‘screw’…

I screw the screw with a screwdriver.

See the confusion??? That’s all it is.

I once got in debate with someone about how C++ doesn’t have interfaces, and through out the debate I was trying to explain how C++ does, it just doesn’t have these keywords or the data type. But the concept was there, and you could perform it. The act of extending abstract classes is basically the same idea as implementing an interface. Just abstractly… the only difference was that the same data type could be used to extend and implement certain Classes as well. Still though it’s all there. The same goes for AS3 (which I bring up a lot), it has an interface data type, but you don’t have to use it. I’ve found several open-source projects on the web where the programmer uses an Abstract Class to define the interface. The rule this abstract class is enforcing is the same exact thing as the interface data type.

4 thoughts on “I screw the screw with a screwdriver – Interfaces

  1. I prefer wring abstracts in AS3 as the interface object is lacking. you can only define properties via getters and setters in an interface and while they have there purpose they are slower at runtime compared to directly setting a class member. AS3 does not permit function overloading yet it permits multiple implementations of different interface objects , and if any of those different interfaces been implemented have a method definition with the same name you will get an error. On many occasions I have decided to write some interfaces for a code library of sorts
    and as the library grew I always seem to find that you can achieve anything via composition of a module as opposed to implementing some interface to gain the shared functionality concept , well thats just my experience really , I cant voucher for everyones experience and Im sure most uber framework flex ninjas will attempt my assassination for this interface blasphemy soonest ;)

  2. ian – note you’re referring to the AS3 interface specifically. Everything you outline there is unique to AS3 in general. And this article is more about describing interfaces in the abstract across several languages.

    Furthermore, interfaces have gains in other situations that your abstracts don’t cover. For instance composition with contract conservation (you wrap an object with another class, then pass operation through by implementing an interface… think IEvenetDispatcher). Or multiple inheritance.

  3. An interface has NO implementation. Abstract classes have SOME implementation. Interfaces are purely high-level design.

    It’s like how your C++ classes had all the functions defined in a “.h” file, and the actual, fleshed-out code in a “.cpp” file, separating design from implementation (if you had actually taken classes, instead of reading books, you might know this).

    And why are you looking at dictionary definitions of words? It’s jargon. Kind of like how the words “object” or “class” mean totally different things in the dictionary vs. use in computing.

    Man, maybe you should stick to math.

  4. … not at my normal computer right now…

    @Bob Dole – Abstract classes don’t have to implement some code, they just can. I can easily create an abstract class with NO implementation.

    This right here proves you completely missed the point I was making. Programming to an interface doesn’t necessarily require the use of an Interface data type. Abstract classes enforce the same exact concepts that Interfaces do, just in a more obfuscated manner. My point is that C++ DOES have the ability to define an interface, and to do so you create an abstract class with little to no implementation, and then extend it by what ever class is going to implement it. Note that when you extend an Abstract Class in C++, it’s not useful until you override and IMPLEMENT the methods (interface) defined in the abstract class.

    If you had actually applied critical thinking to what you read in classes, instead of just digesting it, you would have understood this.

    And I reference definitions because jargon is chosen due to the meaning of the word. The designers of computer languages didn’t pick the word ‘interface’ because they thought it sounded pretty, they picked it because the definition could be applied to the concept they’re trying to convey.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>