How to enforce a type on an interface after implementing an interface
I want to create a custom function that enforces the type.
public interface StringGroupFunction implements Function<String, String> {
}
This is not allowed. The only possibility I’ve found is to make StringGroupFunction
an abstract class. Any other ideas?
Solution
This is a common misconception: an interface does not implement another interface, it extends it because it does not provide a body for the function.
If you want to add additional methods to an interface, you have several options. You could create a DoItPlus interface that extends DoIt:
public interface DoItPlus extends DoIt { boolean didItWork(int i, double x, String s); }
You can read about interfaces in the Java specification More information:
If an extends clause is provided, then the interface being declared extends each of the other named interfaces and therefore inherits the member types, methods, and constants of each of the other named interfaces.
These other named interfaces are the direct superinterfaces of the interface being declared.
Any class that implements the declared interface is also considered to implement all the interfaces that this interface extends.