2
Follow the code I’m studying:
public interface Node {
public abstract int eval ();
}
public abstract class Unary implements Node {
private final Node child;
public Unary(final Node child) {
this.child = child;
}
public final int eval () {
return compute(child.eval());
}
protected abstract int compute(int c);
}
public abstract class Binary implements Node {
private final Node left;
private final Node right;
public Binary(final Node left, final Node right) {
this.left = left;
this.right = right;
}
public final int eval() {
return compute(left.eval(), right.eval()); // minha dúvida aqui, porque left.eval()
// ao invés de só left?
}
protected abstract int compute(int l, int r);
}
My question is, to the class Binary
, in the method eval ()
when he returns compute(left.eval(), right.eval())
, because the variables left
and right
are followed by the method eval()
interface? whereas interface methods do nothing.
They "do nothing" on the interface
Node
. But if you pass an object that implements the interfaceNode
, and creates an implementation for the methodeval
, it will return (or must return) an integer.– Gustavo Sampaio
Related: https://answall.com/q/2913/112052
– hkotsubo