Polymorphism

Post on 22-May-2015

507 views 3 download

Tags:

description

Tried to explain basics of polymorphism using easy example.

Transcript of Polymorphism

Circle

Rectangle

Triangle

DrawPolymorphism

Bhushan Mulmulebhushan.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

Abstract Class Implementation

Interface Implementation

or

or

Let us focus on first technique in this session

Polymorphism using Inheritance, Upcasting and Late Binding

www.dotnetvideotutorial.com

Upcasting is holding derived class object in base class reference

If we have this class hierarchy

we generally create objects…

www.dotnetvideotutorial.com

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

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

sshape shape

Circle

Rectangle

Shape s = new Shape();

s = new Circle();

s = new Rectangle();

www.dotnetvideotutorial.com

Now let us try to understand Late Binding

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

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 Shadowing.

Note: new keyword just confirms that you are intentionally shadowing Draw() method in derived class.

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

Then the output that we are expecting is…

Expected Output

www.dotnetvideotutorial.com

Output is surprising…

But What’s this?

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 doesn't care about type of object. Actually there are no objects in memory at compile time

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

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

What's the solution?

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 using override keyword and

can be modified

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

ability of different objects

to respond same message

In different ways

Polymorphism is

For Video visitwww.dotnetvideotutorial.com

Bhushan Mulmulebhushan.mulmule@dotnetvideotutorial.com

www.dotnetvideotutorial.com