Why do people have trouble learning to program using the object-oriented paradigm? On the surface, it seems so obvious. This approach corresponds with the way we view things in the world—as objects with properties and behaviors. It should be more natural to learn OO programming than procedural. I suggest the way OO programming is initially presented is a significant part of the problem.
I recently taught an introductory Java class. No matter how often I explained the OO concept, some students continued to have difficulties in implementing it in their projects. One student went through the whole quarter writing all the code for his projects in a single class, even though the assignments, lectures, and project feedback clearly indicated that each project had to be implemented with two classes.
I reflected on my personal experiences with OO programming. I started writing code in the mid-1960s, so my past experience was heavily weighted toward procedural languages. Three decades later, I started studying books on Java. As I read them, I had a vague feeling that something was missing. Although the books extolled the virtues of the OO paradigm, the code samples and text were not really communicating the concept. Then I realized most of these books were written by people whose previous backgrounds were in procedural languages. I was not learning "object-think," because the authors were not writing from that perspective. The books didn’t emphasize through example the idea of decomposing problems into individual objects.
As I worked more with Java and some quasi-OO languages, the concept became clearer. It began to feel more natural to create objects to use in my own code, rather than just using objects supplied with languages.
So what is the problem here? In a moment of inspiration I realized that it starts at the beginning. The "Hello, World" program has been a staple of introductory computer programming classes ever since Kernighan and Ritchie’s C book [3] was initially published in 1978. With apologies to Edsger Dijkstra [2], I argue that "Hello, World" or an equivalent program in many Java books is harmful to the development of object-think.
For example, on the first page of The Java Programming Language [1] the following code (identified in the Index as "HelloWorld class example") is presented:
class HelloWorld {
public static void main (string[] args) {
System.out.println("hello, world");
}
}
Although this code runs, it communicates virtually nothing about the concept of user-created objects. Other than the initial occurrence of the key word "class," and that funny little dot in the print statement, it could just as well have been written 25 years ago in some procedural language. It doesn’t instantiate any objects and use any object behaviors.
Once this problem is identified, the solution is simple. The "Hello, World" needs to be rewritten to include a user-created object. All it takes is a few small but significant changes from the traditional program, as indicated:
- Make two copies of the original code.
- Change the name of the method in the first copy from main to something else (say, to printHello) and remove its arguments.
- Change the name of the class in the second copy (to UseHello).
- In the second copy, instantiate an object of the "Hello, World" class defined in the first copy.
- Use the method (printHello) from this instance to print the message.
The result is shown in the following OO "Hello, World" Java code:
class HelloWorld {
public static void printHello() {
System.out.println ("hello, world");
}
}
class UseHello {
public static void main (String[] args) {
HelloWorld myHello = new HelloWorld();
myHello.printHello();
}
}
The important thing is to present this example to learners at the very beginning and have them type it into their own computers right away. This will help imprint the concept immediately, so they do not have to "unlearn" a procedural approach. For those who have already learned a procedural language, this approach emphasizes the differences from the beginning rather than reinforcing existing misconceptions through an example such as in traditional "Hello, World" Java code.
Join the Discussion (0)
Become a Member or Sign In to Post a Comment