As programs become more complex, the advantages of OOPs increase because they are readily scalable. What does that mean? Well, OOPs provide options to more easily define what actions can be taken on a class of objects by a program. This increased ease makes it easier to make bigger and more complex programs.  To illustrate this, have a look at the following example.

If you wanted to add two numbers, say, 1 and 2, in an ordinary, non-object-oriented computer language like C (don't worry -- you don't need to know any C to follow this), you might write this:

a = 1;
b = 2;
c = a + b;

This says,

"Take a, which has the value 1, and b, which has the value 2, and add them together using the C language's built-in addition capability. Take the result, 3, and place it into the variable called c."

Now, here's the same thing expressed in Smalltalk, which is a pure object-oriented language:

a := 1.
b := 2.
c := a + b.

Wait a minute. Except for some minor notational differences, this looks exactly the same! Okay, it is the same, but the meaning is dramatically different.

In Smalltalk, this says,

"Take the object a, which has the value 1, and send it the message "+", which included the argument b, which, in turn, has the value 2. Object a, receive this message and perform the action requested, which is to add the value of the argument to yourself. Create a new object, give this the result, 3, and assign this object to c."

Hmm. This seems like a far more complicated way of accomplishing exactly the same thing! So why bother?

The reason is that objects greatly simplify matters when the data get more complex. Suppose you wanted a data type called list, which is a list of names. In C, list would be defined as a structure.

struct list {
<definition of list structure data here>

list a, b, c;

a = "John Jones";
b = "Suzy Smith";

Let's try to add these new a and b in the C language:

c = a + b;

Guess what? This doesn't work. The C compiler will generate an error when it tries to compile this because it doesn't know what to do with a and b. C compilers just know how to add numbers. Period. But a and b are not numbers. One can do the same thing in Smalltalk, but this time, list is made a class, which is a subclass of the built-in Smalltalk class called "String":

a := List fromString: 'John Jones'.
b := List fromString: 'Suzy Smith'.
c := a + b.

The first two lines simply create List objects a and b from the given strings. This now works, because the list class was created with a method which specifically "knows" how to handle the message "+". For example, it might simply combine the argument with its own object by sticking them together with a comma separating them (this is done with a single line of Smalltalk). So c will have the new value:

'John Jones, Suzy Smith'

Here's another example of a "+" message using more interesting objects. Click a, b, and c in turn to find out what they are (you'll need a WWW browser with support for "aiff" sounds):

c = a + b;

Back To Top


Thanks to Terry Montlick for this example.



Back To Top

Visit An Area

Technophile_City_(INFOTECH)The_Time_Machine_(HISTORY)  |  The_Tree_House_(SOSE)Lessons_Index   |  Assessment_Instruments  |  Maps   |  Power Point   |  Proforma  |  Links  |  Gallery  |  Games  |  Humour   |  Prizes  |  WebRings  |  Soapbox  |  Search_Engines  |  Site_ Map_ |


             Search OzEdweb by Keyword