Java Reference
In-Depth Information
fillables[i] = (Fillable) drawables[i];
fillables[i].fill(Fillable.GREEN);
}
}
Afterinvokingeachdrawable's
draw()
method,
main()
createsa
Fillable
ar-
ray of the same length as the
Drawable
array. It then proceeds to copy each
Draw-
able
array element to a
Fillable
array element, and then invoke the fillable's
fill()
method.The
(Fillable)
castisnecessarybecauseadrawableisnotafil-
lable.Thiscastoperationwillsucceedbecausethe
Point
and
Circle
instancesbeing
copied implement
Fillable
as well as
Drawable
.
Tip
You can list as many interfaces as you need to implement by specifying a
comma-separated list of interface names after
implements
.
Implementing multiple interfaces can lead to name collisions, and the compiler will
reporterrors.Forexample,supposethatyouattempttocompile
Listing2-45
'
sinterface
and class declarations.
Listing 2-45.
Colliding interfaces
interface A
{
int X = 1;
void foo();
}
interface B
{
int X = 1;
int foo();
}
class Collision implements A, B
{
@Override
public void foo();
@Override
public int foo() { return X; }
}