Lorenzo Sciuto
Lorenzo Sciuto

Reputation: 1685

Java - extend an interface

Is it correct to extend an empty interface? I just need to have a method (EventPlayer) with a parameter (EventCLass myEvent) that could be one time a class and the next time another class.

public interface EventClass {
  // ... empty ...
}

public interface EventClassExt1 extends EventClass {

    public void firstEvent();

    public void secondEvent();
}


public interface EventClassExt2 extends EventClass {

    public void thirdEvent(String text);
}

public EventPlayer(final EventCLass myEvent) 

Upvotes: 3

Views: 172

Answers (3)

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

Is it correct to extend a Class that has not other object in it?

I assume by this you mean an empty interface.

This is something that was used in Java a lot before they had annotations to sign a class is of that type ( from Java 5 ).

What you are doing is correct - basically you are marking the extended interfaces/classes type of EventClass but I would use annotation which is the new way to do that

http://tutorials.jenkov.com/java-reflection/annotations.html

Upvotes: 1

Bohemian
Bohemian

Reputation: 425043

Yes it's OK to do that.

When an interface has no methods, it's generally called a marker interface; Serializable is one of many examples of such an interface from the JDK.

Also, you probably don't want "class" in your interface name. Just Event is a better choice.

Upvotes: 2

Kent
Kent

Reputation: 195079

yes it is correct. it is called Marker Interface.

http://en.wikipedia.org/wiki/Marker_interface_pattern

Upvotes: 2

Related Questions