In Objective-C it is possible to pass a function parameter with type in the form Object<Protocol> which has the meaning "an object of type Object that implements the protocol Protocol". I was wondering whether there is a way to do the same in Java, for instance "an Android View that implements some custom interface" (protocol = java interface).
1 Answer
You can use generics to specify that the parameter implements an interface:
public <T extends Protocol> void foo(T t) { ... }
This can be extended to specify multiple bounds. The type must be a subtype of all the specified types. Object isn't the best example, but here's how you could specify it:
public <T extends Object & Protocol> foo(T t) { ... }
Here's a related tutorial:
Oracle Docs - Bounded Type Parameters
Especially note this paragraph:
To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound, which in this example is Number. Note that, in this context, extends is used in a general sense to mean either "extends" (as in classes) or "implements" (as in interfaces).
The unexpected part here is that extends is used for interfaces as well.
Note that you can get the desired behaviour without generics as well (see comments below):
public void foo(Protocol p) { ... }
since this allows for any object that is-a Protocol. If you want to specify multiple interfaces with this approach you could for example create a third interface that implements the other two. However, this assumes that you're in control of the classes that should be able to be passed as arguments since you need to implement that interface.
If you're passing a "regular" generic type parameter you simply specify the type directly:
public void foo(Object<Protocol> o) {
// TODO: Implement
}
9 Comments
<T extends Protocol> foo(T t) adds exactly no benefit over simply using foo(Protocol t) as both methods do the same, they accept an arbitrary object which class implements Protocol. But the latter works since the very first Java release…foo(Protocol t) is a lot more readable than the unnecessary Generics construct.
Xwhich means “an object of whatever type that implementsX” in caseXis an interface.MyClass<MyProtocol>. Is that what you're asking about?