10. where references and objects are of same types
Shape s = new Shape();
s
shape
Circle c = new Circle();
c
Circle
Rectangle r = new Rectangle();
shape
Circle
r
Rectangle
Rectangle
11. As Circle and Rectangle are derived from Shape class.
You can also create object using upcasting…
12. where base class reference can hold
objects of derived class
Shape s = new Shape();
s
shape
shape
s = new Circle();
Circle
s = new Rectangle();
Rectangle
www.dotnetvideotutorial.com
14. Binding is connecting method Invocation to method
implementation
By default .NET use Early Binding …
Early binding is also referred as Static Binding or Compile Time Binding
15. If we have Draw() method in base class and also
redefined it in derived classes
www.dotnetvideotutorial.com
16. Redefining Draw() method
in Circle and Rectangle
Class hides the base class
implementation of Draw().
This is knows as
Shadowing.
Note: new keyword just confirms that you are
intentionally shadowing Draw() method in
derived class.
17. And we have created objects using upcasting
and invoked Draw() method
24. Because by default .NET use early binding.
Compiler simply checks type of reference and bind it
accordingly.
Compiler doesn't care about type of object. Actually there are no objects in
memory at compile time
25. 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
26. What's the solution?
Is it possible to bind method call depending on object type instead
of reference type?
27. And here is the magic keyword
virtual
Call to virtual method gets resolved at runtime depending on actual
object type.
28. 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
29. Now Let us run same client code
www.dotnetvideotutorial.com
30. Will call Draw() of Shape
Will call Draw() of Circle
Will call Draw() of Rectangle