Encapsulation & Polymorphism

October 17, 2009 on 11:03 am | In Programming | No Comments

Most programmers nowadays are educated with Java. As Joel discusses, this eliminates the need to teach many difficult topics such as pointers. Two topics that Joel does not dig into are encapsulation and polymorphism.

Encapsulation is the fundamental concept of Object Oriented Programming (OOP). In C++ and Java, this translates to objects encapsulating tightly coupled data and functions. However, since all messages between objects are function calls, it is very easy for tight coupling to creep in. This is why some people campaign against getters and setters: overuse can lead to tightly coupled data and functions residing in separate objects.

In addition, C++ and Java force concepts to be mixed together because polymorphism is implemented primarily via inheritance. Polymorphism boils down to different behavior in response to the same message. Coupling this with inheritance, which is merely one way to re-use code, requires programmers to either get stuck in a straight jacket of single inheritance or go hog wild with mix-in interfaces. The latter is better, but it can still get very confusing.

To see encapsulation and polymorphism clearly, it helps to consider other implementations.

The purest forms of encapsulation occur when the boundaries are strongest: interacting processes on separate machines, e.g., web services or XHR/JSONRequest. In this context, interaction requires network traffic, and efficient systems strive to minimize this. Interestingly, this does not lead to minimal interfaces such as what you find in C libraries where each function does one small, simple operation. Instead, each interaction accomplishes as much as possible to minimize the network traffic. Getters and setters are non-existent, except in special cases when that is all that is required.

The purest forms of polymorphism occur in weakly typed languages. Objective C allows any message to be sent to any object. If the object does not implement the message, it can either delegate it to another object or throw a run-time error. The same is true for JavaScript, Perl, Python, etc., though language support for delegation varies.

How can we apply this to C++ or Java? Since encapsulation is weakly enforced by the language, it helps to encapsulate functionality, i.e., modules, instead of individual classes. Modules are much less likely to need getters or setters. Instead, they normally expose higher level actions. Messaging frameworks similiar to Objective C can been built in strongly typed languages, e.g., signals-and-slots or JBroadcaster, but it typically requires manually writing or automatically generating dispatching and receiving code.

Along with questions about pointers and functional programming, digging into a person’s understanding of encapsulation and polymorphism makes for a good technical interview.

No Comments yet »

RSS feed for comments on this post.

Leave a comment

You must be logged in to post a comment.

Blogroll

Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds. Valid XHTML and CSS. ^Top^