2

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).

8
  • Very imprecise question Commented Aug 13, 2014 at 12:47
  • Yes, this is called "generics". Commented Aug 13, 2014 at 12:53
  • In Java there is no need for that. Just declare the parameter type as X which means “an object of whatever type that implements X” in case X is an interface. Commented Aug 13, 2014 at 14:06
  • In Objective-C, you can have a type that both is an instance of a type AND conforms to one or more protocols, like MyClass<MyProtocol>. Is that what you're asking about? Commented Aug 13, 2014 at 19:36
  • @newacct That's how I interpreted it. Commented Aug 14, 2014 at 7:57

1 Answer 1

4

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
}
Sign up to request clarification or add additional context in comments.

9 Comments

It’s worth noting that <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…
Lots of programming constructs are about readability rather than explicit benefits. It is worth noting though :)
Exactly, and foo(Protocol t) is a lot more readable than the unnecessary Generics construct.
I partly agree with you. Think of the other possibilities, and generalization. What if there are two interfaces?
@Holger I added the alternative to the answer itself to make it more complete
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.