Polymorphism

35
Circle Rectangle Triangle Draw Polymorphism Bhushan Mulmule [email protected] www.dotnetvideotutorial.com

description

Tried to explain basics of polymorphism using easy example.

Transcript of Polymorphism

Page 1: Polymorphism

Circle

Rectangle

Triangle

DrawPolymorphism

Bhushan [email protected]

www.dotnetvideotutorial.com

Page 2: Polymorphism

Polymorphism is

Page 3: Polymorphism

Ability of different objects to respond same message

in different ways.

Circle

Rectangle

Triangle

Draw

Page 4: Polymorphism

We can achieve it using…

Page 5: Polymorphism

Inheritance, Upcasting and Late Binding

Abstract Class Implementation

Interface Implementation

or

or

Page 6: Polymorphism

Let us focus on first technique in this session

Polymorphism using Inheritance, Upcasting and Late Binding

www.dotnetvideotutorial.com

Page 7: Polymorphism

Upcasting is holding derived class object in base class reference

Page 8: Polymorphism

If we have this class hierarchy

Page 9: Polymorphism

we generally create objects…

www.dotnetvideotutorial.com

Page 10: Polymorphism

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

As Circle and Rectangle are derived from Shape class.

You can also create object using upcasting…

Page 12: Polymorphism

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

Now let us try to understand Late Binding

Page 14: Polymorphism

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

If we have Draw() method in base class and also redefined it in derived classes

www.dotnetvideotutorial.com

Page 16: Polymorphism

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

And we have created objects using upcasting and invoked Draw() method

Page 18: Polymorphism
Page 19: Polymorphism

Then the output that we are expecting is…

Page 20: Polymorphism

Expected Output

www.dotnetvideotutorial.com

Page 21: Polymorphism

Output is surprising…

But What’s this?

Page 22: Polymorphism

Actual Output

www.dotnetvideotutorial.com

Page 23: Polymorphism

Why?

www.dotnetvideotutorial.com

Page 24: Polymorphism

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

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

Is it possible to bind method call depending on object type instead of reference type?

What's the solution?

Page 27: Polymorphism

And here is the magic keyword

virtual

Call to virtual method gets resolved at runtime depending

on actual object type.

Page 28: Polymorphism

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

Now Let us run same client code

www.dotnetvideotutorial.com

Page 30: Polymorphism

Will call Draw() of Shape

Will call Draw() of Circle

Will call Draw() of

Rectangle

Page 31: Polymorphism

And yes this time output will be…

Page 32: Polymorphism

As expected.

www.dotnetvideotutorial.com

Page 33: Polymorphism

ability of different objects

to respond same message

In different ways

Polymorphism is

Page 34: Polymorphism

For Video visitwww.dotnetvideotutorial.com