Advantages of polymorphism?

  • Thread starter Thread starter Danny Ni
  • Start date Start date
D

Danny Ni

Hi,

I was asked about the advantages of polymorphism in a recent job interview.
What are they? I couldn't think of anything, so I answered it's just an
option programmer can make use of in an OO design.

Thanks in Advance
 
Danny Ni said:
Hi,

I was asked about the advantages of polymorphism in a recent job interview.
What are they? I couldn't think of anything, so I answered it's just an
option programmer can make use of in an OO design.

As an example, say you have an office application that contains a Word
document class, an Excel document class, and a PowerPoint document class.
You could design an interface (call it "IDocOutput"), define a Print()
method in that interface, and then declare each of the document classes as
implementing that interface. This forces you to implement a Print() method
in each of the document classes. It also allows you to treat an instance of
any of the three document classes as an instance of IDocOutput. You could
then write a function somewhere called "PrintDocument(IDocOutput theDoc)"
and inside this function simply call "theDoc.Print()". In the
PrintDocument() function you don't need to worry if the document is Word,
Excel, or PowerPoint.

An alternative to doing this without Polymorphism would be to separate print
handler for each document and have some conditional logic to choose between
them. In doing so you would probably have duplicated code, which is not
good, right? Also, by designing an interface to expose only those methods of
a class that are relevent in a given context, you are reducing the
opportunity of doing something bad with an object.

In the end it usually boils down to code reuse, maintainability, and
robustness, and polymorphism helps promote all of those.
 
Danny Ni said:
Hi,

I was asked about the advantages of polymorphism in a recent job interview.
What are they? I couldn't think of anything, so I answered it's just an
option programmer can make use of in an OO design.

Thanks in Advance

Polymorphism is one of the features provided by OOP. Polymorphism
makes it possible to call different mathods using one interface.

For example a coder just need to know the implication of *move* method
on the *vehicle* objects available. He just call *move* method either
it is a Bus, a Car or a Bycycle. And he know what will be the result.

And the polymorphism feature even makes it possible to select
appropriate method implementation of *move* at run-time depending upon
which vehicle it has been called.

This is a very layman description of Polymorphism.

Now the technical part:

A language can implement polymorphism in various forms.

First is method overloading.

For example i can define methods of following signature:-

double Add(int A, int B);
double Add(double A, double B);
double Add(int A, double B);
double Add(double A, int B);

Now depending upon the parameters passed appropriate method will be
called.

Another method is to override the method definition of a base class in
derived class.

This is implemented through *new* keyword in C#.

Another way to implement polymorphism is through *Virtual* keyword.
And through interfaces.

Please visit online MSDN library for more details about these
features.


I think this will help to understand a little about Polymorphism....
 
poly refers to multi
morph refers to faces

Taking multiple advantages from one thing is called polymorphism.
=================================================
you can think of Graph making software , you provide data only once and
then you use tabs to swtich to line graphs , pie charts, histograms etc
================================================
 
Back
Top