Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java Language
Expertise: Beginner
Mar 19, 1997

What is an Interface?

I have no experience with object-oriented languages, having only learned BASIC when I was a child. Most people say Java is easy, but in my case it's not. I don't understand the keyword 'implements', I don't understand 'interface'. Is there anydifference between a class and an interface? Without an interface, are there any functions that a Java programmer can't use?

I think most people would agree that Java isn't all that easy, especially if you come from a BASIC background and have not yet delved deeply into object- oriented programming.

Let me review a few concepts first, before getting to your question.

The basic concept of object-oriented programming is, naturally enough, the object. An object carries with it some data that describe its current state. And each objects has certain functions that it can carry out. For example, an AudioFile object may store a name of a wav file and carry out the function "play".

Objects of the same type are grouped into classes. A class specifies what functions its objects can carry out, and what data they store. For example,

     class AudioFile

     {  // functions

        public void play();

        // data

        private String filename;



By the way, the official Java speak for "function" is "method,"; for "data," it's "instance variable." But I'll stick with the more familiar terms for now.

The idea is that you should not access the object data directly. That is why they are declared private. You read and change the object state through the public functions.

It often happens that one class is a refinement or specialization of another. For example, suppose you design a class ElevatorMusic (for those Java-powered elevators) that keeps playing the audio over and over. To indicate that this class is a special case of an audio file, you use the extends keyword.

     class ElevatorMusic extends AudioFile

     {  public void play();



This says: an ElevatorMusic object is just like an AudioFile object, except the details of the play function are different. In particular, you don't have to figure out how the audio data are stored--the AudioFile class takes care of that. We say that the ElevatorMusic inherits that capability.

This concept of inheritance is powerful because it lets you reuse existing classes. In fact, it is so useful that you may want to inherit from two classes at the same time:

     class ElevatorEntertainment extends AudioFile, NervousText

          // not legal Java

     { . . .



Unfortunately, that is not possible in Java. Some languages, such as C++, support this multiple inheritance, but they pay a high price. The layout of the object data gets very complex when you have more than one parent class. The designers of Java wanted to avoid the complex and murky corners of C++, so they decided not to support multiple inheritance.

But there are times when you really need to inherit from two classes. Every applet must extend the class Applet. Every class that wants to run code in a thread must extend the class Thread. But you can't code:

     class ElevatorApplet extends Applet, Thread

          // not legal Java

     {  public void init() { /* applet initialization code */ }

        public void run() { /* thread code */ }

        . . .



What to do? This is where interfaces come in.

An interface is a class without data, only with functions.

     interface Runnable

     {  public void run();



All the complications of data layout of multiple parents go away when you don't have any data, so Java is happy to let you inherit as many interfaces as you like:

     class ElevatorApplet extends Applet

          implements Runnable, ImageObserver

     {  public void init() { /* applet initialization code */ }

        public void run() { /* thread code */ }

        . . .



In practice, when do you need to worry about interfaces? If you mostly use the existing Java library, you don't usually need to design your own interfaces. But if you write code that other programmers reuse, you may need to supply a few interfaces yourself.

The standard Java library defines 18 interfaces:





















Most of them are extremely technical and are only used to implement other Java library features. The ones you are likely to implement yourself are Runnable (for multithreading) and maybe ImageObserver (to monitor the loading progress of an image).

Occasionally you will handle objects of type Enumeration, AudioClip or DataInput. But as a user you don't care that these are really interfaces, not classes.

So, here is the answer to your question in a nutshell:

An interface is a class without data You use "implements" to inherit an interface, "extends" to inherit a class.

In practice, you don't need to worry too much about interfaces. You implement Runnable when you need multithreading, for example for animation. Otherwise, you can leave interfaces to the library builders.

DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date