Let's say I have a method that takes one parameter.
This parameter should match the following requirements:
- 'of type': The method needs to know that the parameter is of a specific class (or subclass).
- 'implements interface': The method needs to know that the parameter implements a specific interface.
I come from iOS development. In Objective C, we can write something like:
- (void)theMethod:(aClass<aProtocol> *)parameter
Which means: The parameter's class is 'aClass' (or a subclass of it) AND it implements 'aProtocol'.
So inside the method, we can call methods of both the class and the protocol safely (the compiler warns us if the object does not match the requirements).
How can I achieve such a method signature in Java? Is it even possible?
Maybe a more concrete example can show you what I'm looking for:
For my Android application, I display DataItems in a GridView. I have a GridViewAdapter which fills the view through an array of DataItems.DataItem has several subclasses and is somewhat 'abstract', because it is never used directly but rather defines the common interface of its subclasses.
Not all DataItems are displayable in a GridView, so there is the GridViewItem interface which needs to be conformed to in order to be displayed in a GridView by the adapter.
Currently, the default constructor of GridViewAdapter looks like this:
public GridViewAdapter(ArrayList<GridViewItem> items).
What I want / need is something like this:
public GridViewAdapter(ArrayList<[DataItem (or subclass) conforming to GridViewItem]>.
I need to make sure that the adapter only uses DataItems that conform to GridViewItem.