Method bodies aren't copied in the undefined method body of a subclass. Instead, when you call
B test = new B();
test.foo();
It will look trough its hierarchy, going up a level every time it can't find an implementation.
First it will check B
which has no implementation. One level above that there's A
which does, so it will use that one.