I have the following method:
private void setFilledAndAdd(Shape obj, Color col, int x, int y) {
obj.setFilled(true); // needs interface Fillable
obj.setFillColor(col);
add(obj, x, y); // needs children of Shape (or Shape itself)
}
If I add one of the lines:
setFilledAndAdd(oval, color, x, y);
Compile time error apears in line obj.setFilled(true); and lineobj.setFillColor(col);. Because Shape is not Fillable. Undefined for the type Shape.
Changing argument type in method setFilledAndAdd for Fillable (not Shape) leads to compile time error in line add(obj, x, y);. It needs Shape in this case.
All children of Shape I use are Fillable.
Give me a hint, how to get this method working.
Thanks.
You can use generics to say that you expect an object that has both characteristics
private <T extends Shape & Fillable> void setFilledAndAdd(T obj, Color color, int x, int y){
obj.setFilled(true); // needs interface Fillable
obj.setFillColor(color);
add(obj, x, y);
}
private void add(Shape s, int x, int y){
// whatever code you have goes here.
}
This compiles just fine for me.
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