Today’s blog is much less the normal in that I will not be providing any coding or examples of anything I have come across. Today, I am going to talk about an interesting topic that is both fundamental to software development in the OOP world, but often misunderstood. Me Included. Yes I know, I shouldn’t admit it, but I also even though thinking I really understood polymorphism and have answered correctly on any interview question when asked what it is, failed the test when reading this blog by an excellent blogger named John Sonmez.
The test was “Can you provide an actual analogy for polymorphism”. The key word here is analogy, not example. I have often said (as taught by my awesome parents long ago), if you can give an analogy for something, then you truly understand it. If not, and can only come up with an example or rigid definition, chances are, you don’t really understand the concept. This doesn’t just apply to programming, but really anything.
So what does John say is a good analogy to polymorphism? A Blu-ray player. Now you are probably thinking like I was… huh?? Well, it actually makes sense when you think about it for a minute. what does a player do? it plays Blu-ray discs, but it also plays regular DVD’s right? So the player acts as the interface to the media (DVD’s and Blu-Ray Discs in this case). In each scenario of placing the different media in the player, a different implementation takes place ie. a different process is done to “play” the movie. So Blu-ray player would be the interface / abstract class, the DVD and Blu-ray Disc would be the classes that inherit from Blu-ray player and then Blu-ray player would contain a method called “Play()” that both Disc classes would be required to implement.
It was an interesting enough article that I thought would be a great thing to send out to my readers and talk a little about what he is explaining so that next time you need to explain what polymorphism is, you are ready to impress.
As always Happy Coding…. and happy Memorial Day a few days early