Web Development
Methods of Polymorphism
2/17/2009 10:03:06 AM
My friend, Matthew Turland, recently sent me a link to blog article arguing that PHP wasn't object-oriented.  When I was reading the response he wrote to the article, I noticed he mentioned polymorphism. This made me think about the arguments one can make on whether a language supports "pure" polymorphism.

My first real experience with polymorphism in a static language has come from a recent project here at Bizzuka. I decided that simply creating objects was the best way to set up the multi-layered configuration required by the project.

Programming in Ruby makes up most of my experience with polymorphism, and I had to get used to a different way of thinking about it.  Ruby supports polymorphism with "duck typing." As long as an object doesn't throw a NoMethodError when a method is called, then the runtime believes that object is of that type. In other words, if it walks like a duck and it quacks like a duck, it's a duck.  This late binding really frees up what you can do with an object.

Unlike Ruby's more flexible approach, C# uses polymorphism to achieved through inheritance. If you want to make two or three objects that have the same method interchangeable, you must make all those objects inherit from the same thing. While this isn't that difficult to do with interfaces, it can sometimes be problematic to remember that the method must exist in the interface.

I personally tend to view the dynamic type of polymorphism as "pure" polymorphism. This truly allows any object to take the place of any other object. Although this is sometimes seen as a pitfall of dynamically typed languages, it is also a large advantage. For example, Objective-C uses this type of polymorphism to make adding plugins to an application easy. All that is required is that the principal class of the bundle implements some method you would like to call. Objective-C also uses this to make delegation extremely easy. If you really wanted to, a subclass of NSString could be the delegate for an NSTableView in your interface. All that is required is that you implement the right methods, and no inheritance is required.

The strongly typed polymorphism that C# uses of course has its advantages. If you feel more comfortable when you have type checking, then this is definitely the way for you. Just be prepared to implement a good amount of interfaces.

Which type of polymorphism do you prefer, and why?
Posted by: Ryan Farnell | Submit comment | Tell a friend

Categories: Software Development

Share and enjoy: Del.icio.us   Facebook   Furl   Google Bookmarks   NewsVine   Reddit   SlashDot   Technorati   Windows Live Bookmark   Yahoo Bookmark
0 Comments
Submit a Comment
Name
E-mail
Web Site  (optional)
Comment
HTML tags are not allowed.

 

© Copyright 2010, Bizzuka - Blog. All rights reserved.