Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
×

# Polymorphism

979 views

Published on

Tried to explain basics of polymorphism using easy example.

Published in: Technology, Business
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

### 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