I would like to use a generic class as a type parameter for another generic class.
At first my class definition was like this:
class Foo<T, R extends BaseDto> {}
Then my requirements changed and I had to use a wrapper/holder class for my R type
My attempt so far: (Gives compile-time error: class or interface expected on Bar<R>)
class Bar<T extends BaseDto> {
private T dto;
public Bar(T dto) {
this.dto= dto;
}
// getters and setters
}
class Foo<T, Bar<R>> {
private T entity;
private R dto;
// getters and setters
}
I can not use it as Foo<T, Object> because then the clients using this class would not know that they actually need a cast from Object to Bar.
How would I implement such behaviour?
Instead of changing the class header, replace everywhere in the class where you used R with Bar<R>.
So the class header stays the same:
class Foo<T, R extends CustomClass>
But let's say you have a field of type R. That needs to be changed to Bar<R>:
Bar<R> someField;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With