Do interfaces have semantics?

Posted by    |      

Several times, I've encountered the following view:

Interfaces in Java don't have any semantics attached. Only a concrete class can define the semantics of a method.

I've always thought that this was a very strange thing to believe. The /semantics/ of an object or operation are whatever a human user understands the object or operation to represent. The /implementation/ of an operation is primarily a set of instructions to the machine - and of course the machine has no use for semantics.

So if the semantics of an object or operation do not reside in the implementation of the object or method, exactly where are the semantics defined?

Well, I would argue that the semantics are declared in two places:

  1. The names of things
  2. The JavaDoc

These are the places where we find /English/ words in our code; therefore, they are meant for /human/ consumption, not machine consumption. The machine is every bit as confortable with a method called A$_128__() of a class named a$aaBxw as it is with the submit() method of Order. Only semantics need to be expressed in English.

And when we look at a Java interface, we see that it contains very little extra information beyond a list of names and JavaDoc. So, in fact, an interface is as close to a pure declaration of semantics as you're ever going to find in Java source code! If interfaces don't have semantics:

  1. How would we know what to call them?
  2. What would we write in the JavaDoc?

Of course, an implementation of an interface can add additional semantics - indeed, any subtype of an abstract type /must/, by definition, add additional semantics. But that doesn't mean that the most abstract type in a type heirarchy has /no/ semantics.

Well, except, perhaps, for Object. Does Object have semantics? It's operations certainly do, but what about the class itself? Let's pretend for a second that Java's type system is not completely broken: there are no primitive types, so all types extend Object. Then does it /mean/ anything to know that something is an Object? I'm not sure. Let's leave that as an exercise for the comment thread ;-)

But one thing I /am/ certain about: when we declare that our object implements an interface, we're definitely saying something /meaningful/ about what kind of thing it is.

Back to top