I have to code a school project, to do that, they gave us some interfaces to help us to design the class implementations.
INode is one of them :
public interface INode { void buildString(StringBuilder builder, int tabs); } We have to implement this interface several times.
BlockNode is one of them :
public class BlockNode implements INode { @Override public void buildString(StringBuilder builder, int tabs) { } } Now my problem is that in the main function they do that (the type of parse is INode):
root = parser.parse(); builder = new StringBuilder(); builder.append("PARSE TREE:\n"); root.buildString(builder, 0); I don't get which implementation of
buildString(StringBuilder builder, int tabs) is called. It could be the one I wrote above (BlockNode) ? or any other I implemented ? I don't understand which one is called in first..
INodeis returned byparse(), of course. You can't tell by inspecting this code.parserto see whatparse()returns we cannot tell you. The whole point of using interfaces is thatparse()returns an appropriate implementation that provides the necessary functionality, and you don't actually need to know which one.parse()is doing, nor what the differences are between your concrete classes (i.e.BlockNodeand the other ones). You have to decide which implementation to return based on your requirements.