Section 10.1 Interfaces in General
In this chapter we will tackle a fundamental concept not just in programming but all throughout computer science and even life in general. And that concept is interfaces.
Merriam-Webster offers a succinct and quite accurate definition of interface:
1
www.merriam-webster.com/dictionary/interface
An interface is the place at which independent and often unrelated systems meet and act on or communicate with each other, as well as the means by which this communication is achieved.
This is a quite broad definition, but a fairly accurate one. I would phrase it a bit differently:
Interfaces are the means of communication at the boundaries where entities or systems interact. They typically involve a certain agreed upon and common understanding of their form and components by both sides of the boundary.The most critical property of interfaces is that they allow the systems to freely change the ways in which they are implemented and operate, as long as they still follow the same interface when communicating with other systems.
As a slightly far-fetched example, the language of this text that you are reading is in effect an interface. We have agreed on the shapes of letters, how those letters come together to form words, the meaning of those words, making meaning out of sentences etc. I am using this interface right now to communicate with you. I may have written this text with a typewriter, a MacBook, maybe I used a tablet, maybe I had someone translate my Greek text to English. You are reading this text on a computer screen, or printed out, wearing glasses or perhaps not, alone or with friends, maybe as an audio-book. And none of these details matter, as long as we have agreed upon how we form letters, words and sentences and the meaning of those words. And as long as we both follow that interface, we can communicate. But if I were to deviate from these agreed upon arrangements and start scrambling the letters within words, mess with the spacing between words, use made-up words without defining them, use my own sentence structure etc, it would be at the very least a lot harder to communicate, if not impossible. Interfaces allow systems to communicate while maintaining their independence.
As another example, the fundamental ways that cars and driving work form another interface, rather a collection of interfaces. When you drive a new car for the first time you may have to figure out how to turn on the windshield wipers or how to look for a new station in the radio, or I guess these days how to hook up your phone to the sound system. But you would have no trouble driving the car around because we have a common understanding of how the steering wheel, gas and brake pedals work. And we also have agreed upon rules for driving our cars on streets with other cars and pedestrians present. Imagine if your steering wheel was turning the car the other way around, or if the gas and brake pedals were reversed. If a car company introduced such a car, it wouldn’t work out all that well.
This common interface is also what allows us to have driving licenses. Your license certifies that you have a solid understanding of this interface, both at the level of a car as well as the level of the rules of the road at the country where you acquired the license. And it lets you drive any car that satisfies that interface, which luckily for us is pretty much all regular cars. But it won’t let you drive a commercial vehicle for example, as those vehicles have other interfaces and regulations.
At a more close-to-home level, you are no doubt familiar with the standard mouse/trackpad, keyboard and screen interfaces that we use in order to work with computers. These were not always so standard, in fact learning to use the mouse was quite non-trivial for early computer users, who tried to place the mouse directly on their screens. And these systems operate in pretty much the same way regardless of whether you are using a mac, windows or linux system. If moving the mouse around had the opposite effect on the screen pointer (left instead of right for example), that would make for a very difficult to use mouse.
Even more close to home, when we write and execute a program in a programming language, we are relying on a precise interface that specifies what a valid program in that language looks like, and we are also relying on the implementers of this language’s compiler to have correctly implemented that interface, so that when we write a
while
loop a predictable behavior occurs. And it doesn’t matter what implementation language they wrote that compiler in, it doesn’t matter what specific instruction set architecture we will compile the program to etc. And we build rocket launchers and nuclear plant software using these languages.