I have blocks performing calculations using a function step()
. The blocks can be connected to
each other with connect(Block)
.
interface Block {
void connect(Block b);
void step();
}
However from within a concrete block implementation (e.g. in step
) it should be
possible to read
from the connected block:
class ABlockImpl implements Block {
private Block src; // link to the block this block is connected to
public void connect(Block b) {
src = b;
}
public void step() {
double x = src.read(); // XXX src is of type Block and there is no read() in Block
/* ... */
}
public double read() {
return 3.14;
}
}
Since there is no read()
in Block
, this won't compile. For clients the "public" Block interface is sufficient, I need read
only internally. I could add read
to the Block interface, but to me this feels wrong.
Since there are multiple different implementations of Block, I cannot cast src
to ABlockImpl
before the call to read
.
Is there an alternative way to "hide" read
?