Chapter 11: Interfaces


The telephone is one of the many technologies we take for granted. You can go anywhere in the country and use a telephone to call home. You don t think twice about how to use the phone because, regardless of the manufacturer, all telephones have the same keypad and work the same way ”they all have the same user interface. Anyone who learns how to use one phone can use the same skills to use any other phone. When you dial home, you are connected in a second, regardless of the technology used in the telephone. The concept of a common interface also applies to an object-oriented program, although not necessarily a user interface. In this chapter you ll explore interfaces used in object-oriented programming and how they increase the flexibility of program development.

Inside Interfaces

When a person in the computer industry hears the term interface , he usually thinks of a user interface, such as the Windows or Mac OS X operating system shells . Interfaces in programming serve a similar need, but at a very different level.

A user interface defines how a user interacts with a program. Thanks to modern graphical user interfaces (GUIs), users know how to exit or terminate the programs on their systems, even if it s the first time they ve run a particular program. This standard approach of various programs on the same operating system leads to a reduced learning curve for new applications.

The interfaces we are interested in, however, are not user interfaces but rather interfaces for object-oriented programming and design. Simply put, an interface defines a set of methods needed to provide a particular feature or behavior in a class. An interface may also provide any number of attributes, but these are not required.

Because an interface defines a certain behavior or feature, we will be using it to add a behavior to a generic class. We can then write methods that take as a parameter the interface, but we can pass to that parameter the class that implements the interface.

Interfaces are supported syntactically by a number of languages, but we will be discussing only Java and C# here. C++ doesn t provide direct support for interfaces; however, there are some tricks we can perform to simulate their behavior.

Interfaces Are Not Classes

An interface might initially start to look like a class. Let s consider the previous definition a bit closer: Simply put, an interface defines a set of methods needed to provide a particular feature or behavior in a class. Right about now you might be thinking, Okay, so let s see here something that defines a set of methods. Sounds like a class to me.

Ok, you got us there. Let s clarify a bit further: Although interfaces define methods, they do not contain any actual code. Fine, now you re thinking, Are you wasting my time? I can create a class with methods, and make all the methods ˜pure virtual in C++ or ˜abstract in Java, and have the same thing. And a very astute observation on your part that is. Interfaces are very similar to classes that contain nothing but abstract methods.

But, consider this: Java and C# do not support multiple inheritance. This means that if you were to create two such classes with only abstract methods, you couldn t use both of them as your base class. And this is where, syntactically, interfaces are different.

Although Java and C# don t support multiple inheritance, they do support multiple interfaces. Therefore, if we wanted to define one interface to provide a behavior such as generating HTML output, and another interface to provide a behavior such as storing or retrieving itself to or from a database, we could do it.

Rather than create a class derived from another class with all abstract methods in it, we create a class that implements an interface. Hmmm, you might be thinking, if an interface is like a behavior or feature, then I guess something that implements the interface could be said to ˜implement a behavior or ˜implement a feature. Why, then, don t they call an interface a feature or a behavior? Well, if they called it a behavior, someone would have said, Gee, that sounds like an interface to me.

In summary, because an interface doesn t provide any code and requires you to write code in your class, this isn t inheritance. Therefore, multiple interfaces don t qualify as multiple inheritance.




OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net