Justin a.k.a. The Code Monkey

Code Monkey [kohd muhng'-kee] n : 1. Creature known for it's ability to transform caffeine into code. 2. Justin Jones
posts - 10 , comments - 27 , trackbacks - 0

Can you tell me the meaning of the word "Polymorphism"?

This is one interview question I ask in every interview, and I get a lot of grief for it.  I've done it for years.  I used to work for a guy who was primarily a Delphi developer, and he as much as ordered me to not ask that anymore.  I think it's a fair question.  People who work in IT using an object oriented language should have a basic grasp on what the three tenets of Object-Oriented Programming are.  If you work in an object-oriented language, you know what they are, but you may not know what they're called.

  • Encapsulation: This is basically information hiding.  All it means is that data internal to the class that other classes have no business messing with are inaccessible to the naughty classes trying to mess with them.  That's what the "private" and "protected" keywords are for.  If you make everything public, you might want to go pick up the old Bjarne Stroustrup book.  This is your craft, time to learn it.
  • Inheritance: This one is easy.  It's what comes after the colon in your class declaration (in C-style languages, anyway).  Most everybody knows what this is.  One hopes they do, anyway.
  • Polymorphism: This is the one everybody forgets.  It's also the hardest one to describe.  In layman's terms it means you have a virtual function/property/whatever declared in an ancestor class that is overridden in a descendant class.  For instance, if you have a Shape base class with descendants Square, Rectangle, and Circle, you might have an Area() method on the base class that is implemented differently by each descendant class.  A Shape pointer/reference that actually points to a Circle will calculate (Pi)r^2.  A Square will calculate W^2, and a Rectangle will calculate L*W.  The more common example is the Employee base class that calculates salary differently for hourly and salaried employees, without the need to know what type they are.  You just automatically get the right one because the method is virtual.

Most people are familiar by now with how virtual methods work in their favorite OO language, but most people don't know there's a computer science term associated with that behavior.  That's why I ask this question.  I don't expect most people to get it right, it's about the reaction to the question.

The results to this question are always fascinating.

At a previous job, one candidate answered that he wasn't sure if he had the right answer, but tried anyway.  He went on to describe Encapsulation.  I gave him credit, because he was A) honest that he wasn't sure, and B) I was pretty sure he knew the answer at one time, because describing Encapsulation means he actually learned the three tenets at some point.  We hired him.  He realized what a nuthouse he'd gotten himself into and left.  C'est la vie.

I told a friend of mine about this question and she used it in an interview.  The interviewee responded "Oh, that's just a buzz word".  That's the kind of thing that sets me off.  You can't possibly think I would ask a question I didn't know the answer to?  Don't bluff.  Calling one of the staples of computer science a "buzz word" shows that A) you're bluffing, B) you don't really know, and C) you've dismissed it as something not necessary to learn to do this grunt work thing called programming.  Any monkey can do this, right?  Calling polymorphism a buzz word is a bit like saying the X-Ray is a passing fad in the medical field.  This kind of answer tells me that coding is not a craft to this person, it's a 9-5 job.  Programmer is not what they are, it's just their job.  That doesn't make them a bad person necessarily, just not somebody I want to work with.

A more recent interviewee responded simply "I don't know".  Surprisingly, this is a good answer.  It shows honesty and integrity.  It tells me that I can count on a straight answer from you when I need it.  Besides, I was pretty sure he knew it, he just didn't know it's name.  There's a Taoist tenet that states "knowing gets in the way of learning."  This sounds absurd at first, as do most Taoist tenets, but someday it will click, if it hasn't already.  Admitting that you don't know means you're able to learn it.  That's a good thing.

You can learn a lot from an interviewee from this question.  Short of having them actually write code (which I think we should), it's one of the best ways to cut through to what kind of programmer this person is.

I've been talking with my boss recently that I think the interview process for developers is a bit flawed.  We hire people based on their ability to interview well, not their ability to write good code.  Jeff Atwood of Coding Horror wrote last year about the FizzBuzz test.  Jeff is one of my favorite bloggers.  He takes his craft seriously and has trouble with people who don't.  He defines the type of person I want to work with.  Luckily, most of the people I work with currently qualify.  I've suggested (seriously, even thought I think it wasn't taken as such) that we should administer the FizzBuzz test.  The test is simple. 

Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".

Since I work with good developers, I find it hard to believe that 199 out of 200 applicants could not pass this test.  I would hope most of the candidates I see could.  Maybe they can't.  This test would tell us a few things though.

A) Whether or not they can write code at all.  That's a good thing to know when you're hiring a developer at near those kinds of salaries.  Some people interview really well, and can't write code to save their mother.  That's not necessarily easy to glean in a 30 minute interview unless you ask them to write code.

B) How they code.  Truthfully, there's more than one way to solve the FizzBuzz test.  The "obvious" choice is to use the modulus operator, but there's always another way.  Say using counters.  How the applicant solves the problem is telling.  Also, seeing them code the answer gives you a clue to how they work, something you can't tell from a finished product, even if they get it right.

Anyway, there's no summary point to this, just a final suggestion.  Take your craft seriously.  It is a craft, it is an art.  Your college COBOL teacher was wrong.  It's what we are, not just what we do.  If you don't "go try that out at home" occasionally, if you don't have some sort of IDE installed on your home PC, you might reconsider is this is the right career for you.  On the other hand, if you're reading this blog, it's highly unlikely that you fall into that category.

Print | posted on Friday, January 23, 2009 12:05 AM | Filed Under [ Computer Science Coding as an art ]

Feedback

Gravatar

# re: Can you tell me the meaning of the word "Polymorphism"?

Yeah, I think that if you have a four year degree in Computer Science obtained in the last several decades, you should know what polymorphism is, plain and simple. Not just the idea, but the term as well. It is not a buzzward - "Ruby on Rails" is a buzzword. I can't imagine a CS curriculum that didn't highly emphasize this central concept of OO programming. If you don't understand it, you are going to run into problems down the line; same with things like vtables, struct alignment, and garbage collection.
1/25/2009 10:33 AM | Luke Sandell
Gravatar

# re: Can you tell me the meaning of the word "Polymorphism"?

um.. well.. there just one thing... er ...um... Your definition of "Polymorphism" is ...well.. wrong....

OK, not so much wrong as merely describing how Polymorphism is implemented in a C++-esque system. But the guiding principle of C++ (and OO and, for the most part, programming) is "Implementation is irrelevant"

A proper (if a bit high level) definition of Polymorphism would be:

"Polymorphism is the ability of different object to behave differently to the same message."

non-"virtual" method are just a C++ optimization. In a true OO system, all methods are "virtual". And in a true OO system, all objects have a common ancestor (namely "Object"). In strict OO, File.Open() and Window.Open() is an example of polymorphism, which would be a bit of a strain seeing in your description.
1/26/2009 9:27 AM | James Curran
Gravatar

# re: Can you tell me the meaning of the word "Polymorphism"?

I agree with James Curran's comment...

In fact your definition seemed to be to much influenced by a specifical implementation...
1/31/2009 10:44 AM | opensas
Gravatar

# re: Can you tell me the meaning of the word "Polymorphism"?

@Luke Sandell

Ruby on Rails is not a buzzword. Ruby is the language, Rails is the framework. Ruby on Rails is the buzzwords.

Oh wait... ;-)
1/31/2009 12:48 PM | Fadzlan
Gravatar

# re: Can you tell me the meaning of the word "Polymorphism"?

First rule of interviewing: know the answers to the questions you are asking. </troll>

@James Curran, @opensas: I agree with your definition and see it like this:

C++ supports both "static" and "dynamic" polymorphism. Static polymorphism is inherent to OO and doesn't even require inheritance to work. That is, two classes can implement a member function (method) having the same signature. This may not seem like much, but Employee.save(), Customer.save() is a big improvement over employeeSave(), saveCustomer(), IMO. To be fair, this can be achieved even in C somewhat via struct scope and function pointer members.

"Dynamic" polymorphism (vtbl) is even more useful, of course, as it enables "late binding." You might say it's what inheritance was invented for. The OP seems to understand this, but neglects to mention the significant design features that DP affords, like interfaces and ABCs; frameworks and the "hollywood principle"; design patterns and IoC, ... (just a buzzword, meh)

I agree also with the other comments w.r.t. implementation. "virtual" is assumed in many OO langauges. Some others permit late binding via alternate constructs: C# and Delphi have delegates/events/method pointers. Still others support closures. Last but not least, dynamic languages give us "duck-typing."

And finally, why all the hatin' on COBOL? Lots of people still use it, and it continues to evolve. When M$ introduced .NET at a PDC many years ago, it profiled two non-M$ langauge implementations: Eiffel and Fujitsu COBOL. The ASP.NET page written in COBOL was awesome, offering a bridge back to all that legacy code -- what better trick can your language do?

I should get my own blog ;)
2/1/2009 10:12 AM | stevej
Gravatar

# re: Can you tell me the meaning of the word "Polymorphism"?

Hey guys, thanks for the comments. Just a couple of replies:

@James Curran:

I do believe you're right. Thank you for the clarification.

@ the rest

TROLLS! j/k. It wasn't right, but it wasn't wrong either :D Honestly, knowing gets in the way of learning. I learned something here.

@ stevej

I don't hate COBOL, i just like picking on it. My COBOL teacher in college liked to beat the fact into us that programming was a clerical job. All you would do in the real world is translate somebody else's specs into code and there's no creativity involved. Having worked in the industry for a while, I can definitively say that's not true. I don't think I've seen comprehensive specs once. Hence, I pick on COBOL. I don't hate it, but I wouldn't voluntarily work in it either.

I'm also still convinced that "Object Oriented COBOL is an Oxymoron". Just an opinion :D

And yes, you should get your own blog. You're a good writer.
5/17/2009 3:18 PM | Justin
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: