Object-oriented – Is it bad practice to use an interface for categorization only

interfacesobject-oriented

For example:

Say I have classes A, B, C. I have two interfaces, lets call them IAnimal and IDog. IDog inherits from IAnimal. A and B are IDogs, while C is not, but it is an IAnimal.

The important part is that IDog supplies no additional functionality. It is only used to allow A and B, but not C, to be passed as an argument to certain methods.

Is this bad practice?

Best Answer

Interface that has no any member or has exactly same members with another interface that is inherited from same interface called Marker Interface and you are using it as a marker.

It is not bad practice but interface can be replaced by attributes(annotations) if the language that you are using support it.

I can confidently say that it is not bad practice because i have seen heavy usage "Marker Interface" pattern in Orchard Project

Here is sample from Orchard project.

public interface ISingletonDependency : IDependency {}

/// <summary>
/// Base interface for services that may *only* be instantiated in a unit of work.
/// This interface is used to guarantee they are not accidentally referenced by a singleton dependency.
/// </summary>
public interface IUnitOfWorkDependency : IDependency {}

/// <summary>
/// Base interface for services that are instantiated per usage.
/// </summary>
public interface ITransientDependency : IDependency {}

Please refer to Marker Interface.