I am using the GWT library. There is a base class called Widget that all Widgets inherit from. Some Widgets implement certain interfaces (for example HasText), others do not. Sometimes I wish to guarantee that something being passed as an argument to a function is of a certain class AND implements a certain interface.
For example, I wish to have a function that takes a argument X, where X is of the class type Widget AND of the interface type HasText. I wish to have this behavior because only Widgets can be added to Layout containers, and HasText specifies the complete set of behaviors that I actually need from said Widget.
In pseudocode form, it might be:
public void fx(I_AM_A_Widget_AND_IMPLEMENT_INTERFACE_HasText x){
//do stuff with x, which is guaranteed to be a Widget AND implement HasText
}
Is this in any way possible in Java? If there are multiple ways to do so, is there a preferred/better way?
Thank you for both of your solutions. In the end, I decided that the best solution was to simply specify bounded types at the top of the class definition. For example, I used the form
I picked this solution for a few reasons.
The declaration of bounded types in the class header is the best solution I have run across. I would still be interested in knowing if their is a way to permanently declare (so that it might be accessed across different classes) a bounded type, rather than declaring them per class.