I guess this is a duplicated question, but after browsing heaps of related questions, I couldn't find a matching one ... yeah, lame excuse ;)
I'm currently developing a common interface for POIs HSLF/XSLF implementations. The reason for using generics is to support the Iterable interface, where user code need not to downcast to the concrete implementation, i.e. one can decide, if he wants to use the implementation classes or the common interface. Of course without generics the return type narrowing works as expected.
My goal is to minimize the parameter declarations for the user of the classes - see main method. Internally the generic references can more complex.
So I'd like to have something like this: (for the sake of simplicity, I haven't used the Iterable interface, but a different type argument)
/* Update: added static to the classes and removed null definitions to actually have a running example */
public class GenericsTest {
static interface SlideShow {}
static class HSLFSlideShow implements SlideShow {}
static interface Notes<SS extends SlideShow> {}
static class HSLFNotes implements Notes<HSLFSlideShow> {}
static interface Slide<SS extends SlideShow> {
<N extends Notes<SS>> N getNotes();
<N extends Notes<SS>> void setNotes(N n);
}
// compile errors
static class HSLFSlide implements Slide<HSLFSlideShow> {
HSLFNotes notes = new HSLFNotes();
@Override
public HSLFNotes getNotes() { return notes; }
@Override
public void setNotes(HSLFNotes n) { notes = n; }
}
public static void main(String[] args) {
HSLFSlide s = new HSLFSlide();
HSLFNotes n = s.getNotes();
s.setNotes(n);
Slide<HSLFSlideShow> s2 = new HSLFSlide();
Notes<HSLFSlideShow> n2 = s2.getNotes();
}
}
I could get it to work with ... but this seems a bit clumsy:
static interface Slide<SS extends SlideShow, N extends Notes<SS>> {
N getNotes();
void setNotes(N n);
}
static class HSLFSlide implements Slide<HSLFSlideShow,HSLFNotes> {
HSLFNotes notes = new HSLFNotes();
@Override
public HSLFNotes getNotes() { return notes; }
@Override
public void setNotes(HSLFNotes n) { notes = n; }
}
public static void main(String[] args) {
HSLFSlide s = new HSLFSlide();
HSLFNotes n = s.getNotes();
s.setNotes(n);
Slide<HSLFSlideShow,HSLFNotes> s2 = new HSLFSlide();
Notes<HSLFSlideShow> n2 = s2.getNotes();
}
How would you minimize the needed type parameter in the main method (minimum is JDK6)?