1

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.

2 Answers 2

2

If I understand you correctly, what you are looking for is a type parameter with multiple bounds :

public <T extends DataItem & GridViewItem> GridViewAdapter (ArrayList<T> items) 
{

}

Or you might move the type parameter to the class declaration :

public class GridViewAdapter <T extends DataItem & GridViewItem> 
{
    public GridViewAdapter (ArrayList<T> items) 
    {

    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

The parameter's class is 'aClass' (or a subclass of it) AND it implements 'aProtocol'.

You can define a type that meets the criteria.

public interface Protocol {
    //abstract method(s)
}

public class SomeClass implements Protocol {
    //implementations
}

Then, your method signature would be:

public void theMethod(SomeClass parameter) {
    //implementation
}

The theMethod() can take subclasses of SomeClass (which will also implement the interface Protocol)

1 Comment

thanks for your answer. the problem with this is that when i need to introduce an interface 'ListViewItem' which does the same thing for ListViews, I can't, because the DataItem class hierarchy is 'locked' into the 'GridView' path?

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.