4

I am a newbie to Java. I was able to compile the following interface without any errors.

File Name : empty_interface.java

File content :

public interface empty_interface {}

Questions

a) Interface , i believe, is a contract which the implementor has to implement. What would a implementor will implement if it extends the above interface ?

b) Might be related to a)...but here I go...Why would compiler allow a undefined interface to compile successfully ?

arshajii
  • 127,459
  • 24
  • 238
  • 287
UnderDog
  • 3,173
  • 10
  • 32
  • 49
  • 2
    Note that the naming convention in java would give your interface the name `EmptyInterface` or something like that. – keyser Aug 14 '13 at 16:30

3 Answers3

8

There are a number of "marker" interfaces in the JDK already. This just signify something which doesn't need methods.

The most common example is Serializable which signifies the class can be serialized. The library does the rest so no additional methods are required.

An obscure one is RandomAccess which signifies than a List can be accessed randomly in an efficient manner. The Collections.sort() uses it.

Another class is Cloneable which is a marker interface but probably should have had a method

public Object clone();

Since Java 5.0, a better way to add meta information like this is to use Annotations, but these were not available previously.

Here is Jon Skeet's excellent answer to a similar question marker interface in java

Community
  • 1
  • 1
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
4

Empty interfaces are marker interfaces, that satisfy multiple roles.

Serialization requires an instance of a class that implements Serializable. The only reason the interface exists is to mark out non-serializable classes(and those the dev doesn't care about serialization for) by absence, and to make developers of their own classes think as to whether their class is serializable.

Oddly enough Serializable mentions a couple of optional methods.

Another hypothetically-valid but not very-useful use is to accept multiple unrelated classes without accepting all said classes.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
  • 2
    To be more precise, it exists to point out serializable classes, not the other way around :) Of course, it does both. – keyser Aug 14 '13 at 16:28
  • @Keyser Yes, but by implication classes sans interface are either ignored by the dev in terms of serialization or distinctly unserializable. – nanofarad Aug 14 '13 at 16:29
1

The interface isn't undefined, it just has no methods defined for it.

An empty interface typically serves as a flag for whether a class supports some behavior.

Whether this is a good pattern is debated, a good example of this in practice is Cloneable, and Serializable. Cloneable lets you know the class implementing it can be cloned via Object.clone, while Serializable lets you know the implementing class allows serialization.

I personally see nothing wrong with this.

William Morrison
  • 10,953
  • 2
  • 31
  • 48
  • Is naming an interface as IFoo somewhat more of a C++ thing, out of curiosity? – nanofarad Aug 14 '13 at 16:28
  • Yeah it is. I've recently taken to naming my interfaces like this though as it helps me find them easier haha... – William Morrison Aug 14 '13 at 16:29
  • 1
    Ah, I see. I'll have to try that to help my organization. – nanofarad Aug 14 '13 at 16:29
  • Yeah, I'm sure there's debate around whether its good practice, but I personally like it. Its hard to keep file names straight sometimes. – William Morrison Aug 14 '13 at 16:31
  • @WilliamMorrison Yeah well it's only _bad_ practice (according to some) if you can think of a perfect and descriptive name and add an `I` to it for no apparent reason. – keyser Aug 14 '13 at 16:33
  • @Keyser To be clear, you support this naming scheme given a perfect descriptive name doesn't exist? I'm personally curious, I've wondered if I'm breaking some clean code rule with this for some time. – William Morrison Aug 14 '13 at 16:35
  • @WilliamMorrison I'm a student with very little real life experience :) But personally, yes, I support it. – keyser Aug 14 '13 at 16:38
  • @Keyser haha, ok, thank you for your input anyway. You'll be working one day, your opinion is valid. – William Morrison Aug 14 '13 at 16:42