I was taking part in an interview and was asked to explain “late and early binding polymorphism”.
Now I’ve programmed in C++, Java, C# and other OO languages for many years, but I’ve have never (that I can recall) heard of late or early binding polymorphism. Safe to say, I needed to find out more…
Early Binding Polymorphism
Early (also known as static) binding is when we override methods in subclasses, i.e. these are resolved at compile time.
So for example we have a Button which overrides the Click method and this is early or statically bound
public class Window { public virtual void Click() { // handles a click on the window } } public class Button : Window { public override void Click() { // handles a button click } }
Late Binding Polymorphism
Late (also known as dynamic and runtime) binding is when we assign a derived type to it’s base type. So using the previous example code the following would cause late binding to take place, i.e. the virtual method Click is resolved at runtime.
Windows window = new Button(); window.Click();