terjetyl
terjetyl

Reputation: 9565

What is an empty interface used for

I am looking at nServiceBus and came over this interface

namespace NServiceBus
{
    public interface IMessage
    {
    }
}

What is the use of an empty interface?

Upvotes: 52

Views: 42569

Answers (12)

mattcole
mattcole

Reputation: 1229

Also known as a Marker Interface.

Upvotes: 25

mustafamuratcoskun
mustafamuratcoskun

Reputation: 36

An empty interface can be used to classify classes under a specific purpose. (Marker Interface)

Example : Database Entities

public interface IEntity {

}

public class Question implements IEntity {
   // Implementation Goes Here
}

public class Answer implements IEntity {
   // Implementation Goes Here
}

For Instance, If you will be using Generic Repository(ex. IEntityRepository), using generic constraints, you can prevent the classes that do not implement the IEntity interface from being sent by the developers.

Upvotes: 0

hellowahab
hellowahab

Reputation: 2485

Empty interfaces are used to mark the class, at run time type check can be performed using the interfaces.

For example An application of marker interfaces from the Java programming language is the Serializable interface. A class implements this interface to indicate that its non-transient data members can be written to an ObjectOutputStream. The ObjectOutputStream private method writeObject() contains a series of instanceof tests to determine writeability, one of which looks for the Serializable interface. If any of these tests fails, the method throws a NotSerializableException.

Upvotes: 0

Paolo Maresca
Paolo Maresca

Reputation: 7686

An empty interface acts simply as a placeholder for a data type no better specified in its interface behaviour.

In Java, the mechanism of the interface extension represents a good example of use. For example, let's say that we've the following

interface one {}
interface two {}

interface three extends one, two {}

Interface three will inherit the behaviour of 'one' and 'two', and so

class four implements three { ... }

has to specify the two methods, being of type 'three'.

As you can see, from the above example, empty interface can be seen also as a point of multiple inheritance (not allowed in Java).

Hoping this helps to clarify with a further viewpoint.

Upvotes: 2

Mendelt
Mendelt

Reputation: 37483

Usually it's to signal usage of a class. You can implement IMessage to signal that your class is a message. Other code can then use reflection to see if your objects are meant to be used as messages and act accordingly.

This is something that was used in Java a lot before they had annotations. In .Net it's cleaner to use attributes for this.


@Stimpy77 Thanks! I hadn't thought of it that way. I hope you'll allow me to rephrase your comment in a more general way.

Annotations and attributes have to be checked at runtime using reflection. Empty interfaces can be checked at compile-time using the type-system in the compiler. This brings no overhead at runtime at all so it is faster.

Upvotes: 45

André
André

Reputation: 13327

In java Serializable is the perfect example for this. It defines no methods but every class that "implements" it has to make sure, that it is really serializable and holds no reference to things that cannot be serialized, like database connections, open files etc.

Upvotes: 15

dove
dove

Reputation: 20674

Been working with NServiceBus for the past year. While I wouldn't speak for Udi Dahan my understanding is that this interface is indeed used as a marker primarily.

Though I'd suggest you ask the man himself if he'd had thoughts of leaving this for future extension. My bet is no, as the mantra seems to be to keep messages very simple or at least practically platform agnostic.

Others answer well on the more general reasons for empty interfaces.

Upvotes: 1

hhafez
hhafez

Reputation: 39750

Empty interfaces are used to document that the classes that implement a given interface have a certain behaviour

For example in java the Cloneable interface in Java is an empty interface. When a class implements the Cloneable interface you know that you can call run the clone() on it.

Upvotes: 1

Juan Manuel
Juan Manuel

Reputation: 178

They're called "Mark Interfaces" and are meant to signal instances of the marked classes.

For example... in C++ is a common practice to mark as "ICollectible" objects so they can be stored in generic non typed collections.

So like someone over says, they're to signal some object supported behavior, like ability to be collected, serialized, etc.

Upvotes: 1

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421998

Normally it's similar to attributes. Using attributes is a preferred to empty interfaces (at least as much as FxCop is aware). However .NET itself uses some of these interfaces like IRequiresSessionState and IReadOnlySessionState. I think there is performance loss in metadata lookup when you use attributes that made them use interfaces instead.

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500675

In Java, empty interfaces were usually used for "tagging" classes - these days annotations would normally be used.

It's just a way of adding a bit of metadata to a class saying, "This class is suitable for <this> kind of use" even when no common members will be involved.

Upvotes: 12

Filip Ekberg
Filip Ekberg

Reputation: 36287

I'd say its used for "future" reference or if you want to share some objects, meaning you could have 10 classes each implementing this interface.

And have them sent to a function for work on them, but if the interface is empty, I'd say its just "pre"-work.

Upvotes: 0

Related Questions