polymorphism
DESCRIPTION
Tried to explain basics of polymorphism using easy example.TRANSCRIPT
![Page 2: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/2.jpg)
Polymorphism is
![Page 3: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/3.jpg)
Ability of different objects to respond same message
in different ways.
Circle
Rectangle
Triangle
Draw
![Page 4: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/4.jpg)
We can achieve it using…
![Page 5: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/5.jpg)
Inheritance, Upcasting and Late Binding
Abstract Class Implementation
Interface Implementation
or
or
![Page 6: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/6.jpg)
Let us focus on first technique in this session
Polymorphism using Inheritance, Upcasting and Late Binding
www.dotnetvideotutorial.com
![Page 7: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/7.jpg)
Upcasting is holding derived class object in base class reference
![Page 8: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/8.jpg)
If we have this class hierarchy
![Page 9: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/9.jpg)
we generally create objects…
www.dotnetvideotutorial.com
![Page 10: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/10.jpg)
s
c
r
shape
Circle
Rectangle
shape
Circle
Rectangle
Shape s = new Shape();
Circle c = new Circle();
Rectangle r = new Rectangle();
where references and objects are of same types
![Page 11: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/11.jpg)
As Circle and Rectangle are derived from Shape class.
You can also create object using upcasting…
![Page 12: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/12.jpg)
where base class reference can hold objects of derived class
sshape shape
Circle
Rectangle
Shape s = new Shape();
s = new Circle();
s = new Rectangle();
www.dotnetvideotutorial.com
![Page 13: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/13.jpg)
Now let us try to understand Late Binding
![Page 14: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/14.jpg)
By default .NET use Early Binding …
Early binding is also referred as Static Binding or Compile Time Binding
Binding is connecting method Invocation to method implementation
![Page 15: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/15.jpg)
If we have Draw() method in base class and also redefined it in derived classes
www.dotnetvideotutorial.com
![Page 16: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/16.jpg)
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.
![Page 17: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/17.jpg)
And we have created objects using upcasting and invoked Draw() method
![Page 18: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/18.jpg)
![Page 19: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/19.jpg)
Then the output that we are expecting is…
![Page 20: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/20.jpg)
Expected Output
www.dotnetvideotutorial.com
![Page 21: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/21.jpg)
Output is surprising…
But What’s this?
![Page 22: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/22.jpg)
Actual Output
www.dotnetvideotutorial.com
![Page 23: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/23.jpg)
Why?
www.dotnetvideotutorial.com
![Page 24: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/24.jpg)
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
![Page 25: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/25.jpg)
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
![Page 26: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/26.jpg)
Is it possible to bind method call depending on object type instead of reference type?
What's the solution?
![Page 27: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/27.jpg)
And here is the magic keyword
virtual
Call to virtual method gets resolved at runtime depending
on actual object type.
![Page 28: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/28.jpg)
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
![Page 29: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/29.jpg)
Now Let us run same client code
www.dotnetvideotutorial.com
![Page 30: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/30.jpg)
Will call Draw() of Shape
Will call Draw() of Circle
Will call Draw() of
Rectangle
![Page 31: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/31.jpg)
And yes this time output will be…
![Page 32: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/32.jpg)
As expected.
www.dotnetvideotutorial.com
![Page 33: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/33.jpg)
ability of different objects
to respond same message
In different ways
Polymorphism is
![Page 34: Polymorphism](https://reader035.vdocuments.mx/reader035/viewer/2022062704/555e365cd8b42a63048b497c/html5/thumbnails/34.jpg)
For Video visitwww.dotnetvideotutorial.com