What is the purpose for using an empty interface that extends another in Java?

爷,独闯天下 提交于 2019-12-07 03:20:58

问题


I just begin to maintain a Java MVC project which use Java Guice Framework.

In almost the entire code, first developers passed as parameter an empty model interface extending another interface.

Here's the first Interface:

public interface FooModel extends ModelInterface {

}

And the other interface:

 public interface ModelInterface {
        public void addListener(FooListener fooListener);
        void setFoo(boolean blop);
        boolean isFoo();
    }

That does not make any sense for me.

Is there a good reason/pattern use empty interface, in Java? Maybe for Guice?


回答1:


Here is a good motivation for "marker interfaces". They are especially useful in aspect-oriented programming where they give you a point to attach to (and I guess Guice is just such a framework).




回答2:


Only reason i know to use an empty interface would be as a marker interface. There are some examples like java.lang.Cloneable or Serializable.




回答3:


It could be used to mark a class for a specific use.

Example: IRequiresSessionState



来源:https://stackoverflow.com/questions/10590526/what-is-the-purpose-for-using-an-empty-interface-that-extends-another-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!