Jack Twain
Jack Twain

Reputation: 6372

How to define Polymorphism without resorting to examples?

I have a technical interview. To prepare for the interview, I read this article: https://sites.google.com/site/steveyegge2/five-essential-phone-screen-questions.

In the article it mentioned that a good candidate should give a good definition for polymorphism "without resorting to examples". However, I wasn't able to find such a definition. All definitions that I found are complicated.

What is a good definition without using examples?

Upvotes: 2

Views: 270

Answers (3)

leoxtc
leoxtc

Reputation: 1

As explained by Bjarne Stroustrup on http://www.stroustrup.com/glossary.html#Gpolymorphism

"Providing a single interface to entities of different types. virtual functions provide dynamic (run-time) polymorphism through an interface provided by a base class. Overloaded functions and templates provide static (compile-time) polymorphism. TC++PL 12.2.6, 13.6.1, D&E 2.9."

Note: Almost no one have the idea of static polymorphism and people usually only think about dynamic.

Upvotes: 0

TigerBear
TigerBear

Reputation: 2824

With polymorphism, you are able to call the same function names on multiple objects of different types. When you write a program you are bound to the methods of a type that you program against. So, the way to get the most functionality, is to program to a sort of general type. This general type describes general functions. You can then use any specific types that also contain these general functions. The way you can be sure that specific types contain these general functions is by either having the specific types sign a "contract" saying that they promise to contain these general functions, or by having them use inheritance. This contract is also referred to as an interface.

So when you are programming, you have to know the name of a function. Otherwise if you call a function that doesn't exist, your program dies. Polymorphism allows a way for us to be 100% sure that different types of objects contain the same function.

Upvotes: 1

RAEC
RAEC

Reputation: 332

Searched "polymorphism" in google and first result is

programming languages and type theory, polymorphism (from Greek πολύς, polys, "many, much" and μορφή, morphē, "form, shape") is the provision of a single interface to entities of different types. A polymorphic type is a type whose operations can also be applied to values of some other type, or types.

It seems like a pretty simple definition.

Upvotes: 0

Related Questions