Circle

Rectangle

Triangle

Draw

Bhushan Mulmule
bhushan.mulmule@gmail.com

www.dotnetvideotutorial.com
Polymorphism is
Ability of different objects to respond same message
in different ways.

Circle

Rectangle

Triangle

Draw
We can achieve it using…
Inheritance, Upcasting and Late Binding
or

Abstract Class Implementation
or

Interface Implementation
Let us focus on first technique in this session

Polymorphism using Inheritance, Upcasting
and Late Binding

www.dotnetvid...
Upcasting is holding derived class object
in base class reference
If we have this class hierarchy
we generally create objects…

www.dotnetvideotutorial.com
where references and objects are of same types

Shape s = new Shape();

s
shape

Circle c = new Circle();

c
Circle

Recta...
As Circle and Rectangle are derived from Shape class.
You can also create object using upcasting…
where base class reference can hold
objects of derived class

Shape s = new Shape();

s
shape

shape

s = new Circle();
Ci...
Now let us try to understand Late Binding
Binding is connecting method Invocation to method
implementation
By default .NET use Early Binding …

Early binding is als...
If we have Draw() method in base class and also
redefined it in derived classes

www.dotnetvideotutorial.com
Redefining Draw() method
in Circle and Rectangle
Class hides the base class
implementation of Draw().
This is knows as
Sha...
And we have created objects using upcasting
and invoked Draw() method
Then the output that we are expecting is…
Expected Output

www.dotnetvideotutorial.com
But What’s this?
Output is surprising…
Actual Output

www.dotnetvideotutorial.com
Why?

www.dotnetvideotutorial.com
Because by default .NET use early binding.
Compiler simply checks type of reference and bind it
accordingly.
Compiler does...
As s is of Shape type so all the three calls will get binded to
Draw() of Shape class irrespective of Object it is pointin...
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

virtual
Call to virtual method gets resolved at runtime depending on actual
object type.
The virtual keyword marks method for late
or dynamic binding.

Virtual method has to be overridden in a
derived class usin...
Now Let us run same client code

www.dotnetvideotutorial.com
Will call Draw() of Shape

Will call Draw() of Circle

Will call Draw() of Rectangle
And yes this time output will be…
As expected.

www.dotnetvideotutorial.com
Polymorphism is
ability of different objects

to respond same message
In different ways
For Video visit
www.dotnetvideotutorial.com
Bhushan Mulmule
bhushan.mulmule@dotnetvideotutorial.com
www.dotnetvideotutorial.com
Polymorphism
Upcoming SlideShare
Loading in...5
×

Polymorphism

541

Published on

Tried to explain basics of polymorphism using easy example.

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
541
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
45
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Polymorphism

  1. 1. Circle Rectangle Triangle Draw Bhushan Mulmule bhushan.mulmule@gmail.com www.dotnetvideotutorial.com
  2. 2. Polymorphism is
  3. 3. Ability of different objects to respond same message in different ways. Circle Rectangle Triangle Draw
  4. 4. We can achieve it using…
  5. 5. Inheritance, Upcasting and Late Binding or Abstract Class Implementation or Interface Implementation
  6. 6. Let us focus on first technique in this session Polymorphism using Inheritance, Upcasting and Late Binding www.dotnetvideotutorial.com
  7. 7. Upcasting is holding derived class object in base class reference
  8. 8. If we have this class hierarchy
  9. 9. we generally create objects… www.dotnetvideotutorial.com
  10. 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. 11. As Circle and Rectangle are derived from Shape class. You can also create object using upcasting…
  12. 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
  13. 13. Now let us try to understand Late Binding
  14. 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. 15. If we have Draw() method in base class and also redefined it in derived classes www.dotnetvideotutorial.com
  16. 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. 17. And we have created objects using upcasting and invoked Draw() method
  18. 18. Then the output that we are expecting is…
  19. 19. Expected Output www.dotnetvideotutorial.com
  20. 20. But What’s this? Output is surprising…
  21. 21. Actual Output www.dotnetvideotutorial.com
  22. 22. Why? www.dotnetvideotutorial.com
  23. 23. 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
  24. 24. 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
  25. 25. What's the solution? Is it possible to bind method call depending on object type instead of reference type?
  26. 26. And here is the magic keyword virtual Call to virtual method gets resolved at runtime depending on actual object type.
  27. 27. 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
  28. 28. Now Let us run same client code www.dotnetvideotutorial.com
  29. 29. Will call Draw() of Shape Will call Draw() of Circle Will call Draw() of Rectangle
  30. 30. And yes this time output will be…
  31. 31. As expected. www.dotnetvideotutorial.com
  32. 32. Polymorphism is ability of different objects to respond same message In different ways
  33. 33. For Video visit www.dotnetvideotutorial.com
  34. 34. Bhushan Mulmule bhushan.mulmule@dotnetvideotutorial.com www.dotnetvideotutorial.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×