I try to create this class:
public class Class1<T extends Class2<E>> {
...
public E someFunction(T param) {
...
}
...
}
I saw topic [blog]: Java class with 2 generic parameters and in it topic suggest using this solution:
public class Class1<T extends Class2<E extends Class3>, E extends Class3> {
...
public E someFunction(T param) {
...
}
...
}
It is works, but in this case I should using 2 parameters without one:
Class1<Class21<Class31>, Class31> var = ...
In this solution I duplicate class ‘Class31’. Has this task have more simple solution, to using following code:
Class1<Class21<Class31>> var = ...
Update1:
What problem are you trying to solve?
For example, I have class for images.
Images can be single-channel (gray image) and multi-channel (3 channels for RGB, HSB and etc. or 4 channels or RGB and etc. + Alpha channel).
Also, image can save value RGB in byte 0..255 or in float (0.0, 1.0) and it is different type of images.
So, I want to create class for this images and construct it as:
MyImage<MyChannelType3<MyColorTypeInt>> img1 = ...
MyImage<MyChannelType3<MyColorTypeFloat>> img2 = ...
MyImage<MyChannelType1<MyColorTypeFloat>> img3 = ...
and have access to colors in pixel as:
int Red = img1.getPixel(x, y)[0];
float Green = img2.getPixel(x, y)[0];
float Gray = img3.getPixel(x, y);
I think is more useful than:
MyImage<MyChannelType3<MyColorTypeInt>> img1 = ...
int Red = img1.getPixel(x, y).getChannel(0).getValue();
If I will be firs return MyChannelType3, than MyColorTypeInt and then value or return Object and demand casting it to int, float, int[] or float[].
This redundancy is unavoidable in Java.
Explanation: In Java a type parameter is just what the name implies, a formal parameter for a type to be substituted with an actual type. What you require is more. You need
Tto be a parameter for a type constructor. Much like a classes’ constructor produces instances of the class, a type constructor produces types.Listis a type constuctor which given the actual typeStringproduces the typeList<String>.Applying this knowledge to your problem, you would want
Tto be a type constructor which takes typesE <: Class3producing typesT <: Class2<E>where<:is the subtype relation. This might be declared and used as follows.Further reading on the subject can be found in Generics of a Higher Kind (pdf). Beyond that you can have a look at Scala which offers higher kinds.
Edit based on update: A perhaps preferable solution might be to implement a
PixelAccessor.