public class Test
{
public static abstract class Node
{
private Node kid;
public abstract int getN();
public Node(Node kid) { this.kid = kid; }
public final Node copyWithNewChild(Node newKid)
{
return new Node(newKid)
{
public int getN()
{
return Node.this.getN(); //****
}
};
}
}
}
As you can see, I have this base class called Node
, with a method getN()
to be overriden by sub-classes.
Suppose I have a class called RedNode
extending Node
and providing a concrete implementation for getN()
, and that I also have another class called SquareRedNode
extending RedNode
and also providing an implementation for getN()
.
(And certainly, something else could also extend SquareRedNode
(ie., the family tree could grow infinitely))
Now question, how does the compiler figure out which getN()
to call when the copyWithNewChild()
is executed?
(I'm not asking "what" implementation is being picked, which I can easily figure out by compiling/executing the code. I want to know how it is done.)
Thanks,