473,413 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Advantages of polymorphism?

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
Nov 16 '05 #1
3 14528
"Danny Ni" <dn**@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.
Nov 16 '05 #2
"Danny Ni" <dn**@yahoo.com> wrote in message news:<#M**************@TK2MSFTNGP10.phx.gbl>...
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....

--
Rahul Anand
Nov 16 '05 #3

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

"Danny Ni" <dn**@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: bissatch | last post by:
Hi, Generally if I re-use code, I use a function. If I need to use these functions over a number of pages I write the function to an include file where all pages have access. So when should I...
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
3
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming ...
11
by: richard pickworth | last post by:
Can anyone explain polymorphism?(very simply). thanks richard
13
by: Krivenok Dmitry | last post by:
Hello all! Perhaps the most important feature of dynamic polymorphism is ability to handle heterogeneous collections of objects. ("C++ Templates: The Complete Guide" by David Vandevoorde and...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.