Binding is connecting method Invocation to method
By default .NET use Early Binding …
Early binding is also referred as Static Binding or Compile Time Binding
If we have Draw() method in base class and also
redefined it in derived classes
Redefining Draw() method
in Circle and Rectangle
Class hides the base class
implementation of Draw().
This is knows as
Note: new keyword just confirms that you are
intentionally shadowing Draw() method in
And we have created objects using upcasting
and invoked Draw() method
Because by default .NET use early binding.
Compiler simply checks type of reference and bind it
Compiler doesn't care about type of object. Actually there are no objects in
memory at compile time
As s is of Shape type so all the three calls will get binded to
Draw() of Shape class irrespective of Object it is pointing to
What's the solution?
Is it possible to bind method call depending on object type instead
of reference type?
And here is the magic keyword
Call to virtual method gets resolved at runtime depending on actual
The virtual keyword marks method for late
or dynamic binding.
Virtual method has to be overridden in a
derived class using override keyword and
can be modified
Now Let us run same client code
Will call Draw() of Shape
Will call Draw() of Circle
Will call Draw() of Rectangle