I followed this pattern to implement a chain of methods with subclasses in Java. The goal is that I have a method on a superclass, but you can assign a subclass, for example:
interface Screen {
<T extends Screen> T setBrightness(int value);
<T extends Screen> T setContrast(int value);
}
class CrtScreen implements Screen {
@SuppressWarnings("unchecked")
@Override
public <T extends Screen> T setBrightness(int value) {
return (T) this;
}
@SuppressWarnings("unchecked")
@Override
public <T extends Screen> T setContrast(int value) {
return (T) this;
}
}
class ColorCrt extends CrtScreen { }
public static void main(String[] args) {
ColorCrt cc = new ColorCrt().setBrightness(50).setContrast(50);
}
Now I also have container objects to which I want to add my objects, for example:
class Stuff {
Stuff add(Screen s) {
return this;
}
Stuff add(String s) {
return this;
}
Stuff add(int i) {
return this;
}
}
public static void main(String[] args) {
new Stuff().add(new ColorCrt().setBrightness(50).setContrast(50)).add(25).add("...");
}
Now this no longer works in Java 8, giving me the add (Screen) method is ambiguous for the Stuff type. I understand the reason described here. At the moment, I see only two options:
I do not use <T extends
, but simply Screen setBrightness(value)
. I need the ability to assign my implementation class to the appropriate variable, and I must use it when I want to execute the implementation-specific method.
.
( ), Java 7.
Java 8, ? , ?