Cincom

Primer : Object-Oriented Principles (Part 1)


It is impossible to cover the entire spectrum of object-oriented (OO) principles in this tutorial; the subject is much too broad, very abstract and contains a lot of theory. What is interesting to note is that many university-level computer science programs use Smalltalk to teach object-oriented principles because it provides a concrete example of the theory of OO programming.

This primer aims to provide just enough background information on OO programming to make you conversant with the terms. As you work with Smalltalk, your understanding will grow and things will start to make more and more sense. It may get frustrating at times but hang with it. Programming in OO is a mindset and it requires, in some cases, a whole new way of thinking about solving the problem at hand. A traditional programmer, when given an assigment, will ask the question "How will I do that?" whereas a Smalltalk programmer will ask the question "What will do that for me?"

What is an Object?

The most widely accepted definition for an object is "An object is an instance of a class." So the next logical question is "What is a Class?". A class is a definition or template that describes the characteristics of an object. For example, an Integer would be considered a class whereas the number 1 would be a specific instance of the Integer class. Some people refer to a Class as an "object factory". Classes (factories) know how to produce objects. Other metaphors include rubber stamps and blueprints. So using a factory metaphor, think of them in this way - the factory that makes a car is not a car. Classes are designs and objects are the manifestations of what the factory or design or blueprint says to make.

There are 2 major characteristics of an object.
  • Properties
  • Methods
  • Some properties of a car would be color, air bag system, engine type, oil capacity and so on. Some methods of a car would include acceleration, braking, turning, etc. The good thing about OO is that although classes are abstract, objects themselves are concrete things and it's easy to relate to real things. From another perspective, think in terms of a typical spoken/written language. Properties are nouns and methods are verbs.

    There is a saying within the Smalltalk community - "Everything is an object." This is a true statement and because of that, the only way to get something to happen within Smalltalk is to send messages to objects which activate their methods. So let's take this theory and apply it to the Smalltalk language.

    Below will be a series of Smalltalk "statements" or "expressions" (whichever term make you more comfortable), highlighted in yellow, with an object-oriented explanation of how Smalltalk interprets that statement. It is extremely important that you understand the explanation. It may seem a bit wordy or lengthy, but nowhere else will you get a more thorough explanation for what is really going on.

    4 squared

    Four (4) is a number. More specifically, it is a particular type of number called an Integer (integers are whole numbers). In VisualWorks, integers are divided into SmallIntegers and LargeIntegers. So, in OO terms, four (4) is an object and SmallInteger is its class. Another way of saying that would be "4 is an instance of the SmallInteger class". It's like re-wording the sentence "A beagle is a particular type of dog" to "Snoopy is an instance of the Dog class".

    VisualWorks comes delivered with a large class library and this library contains code (methods) that belong to these classes. These methods would perform functions that you would expect a computer (or a computer language) to do. For example, when it comes to numbers, you would expect a computer to know how to add, multiply, subtract and divide. You would also expect it to perform some higher mathematical functions too, with "squared" being one of them.

    So in the above statement, the 4 (instance of the SmallInteger class) was sent the message squared.
    Is it starting to make some sense? Perhaps another example might help.

    'pots' reverse

    The word "pots" is contained within single quotes. To Smalltalk, this is just a sequence of characters otherwise known as a String. String is the class and "pots" is a particular instance of the String class. We could have easily used an example that was not a word, like "wxyz". If you think about it, the word "pots" to Smalltalk is not a "word" at all. To those who speak English, it is a word, but to Smalltalk, it is just a sequence of characters and because it was contained within single quotes, Smalltalk calls it a String. String is the class and "pots" (or "wxyz") is a particular instance of the String class.

    As stated before, VisualWorks comes delievered with a large class library and this library contains code (methods) that belong to these classes. These methods would perform functions that you would expect a computer (or a computer language) to do. For example, when it comes to strings, you would expect a computer to know how to count the number of characters or separate the string into individual characters. The method reverse happens to be one of those. It tells Smalltalk to reverse the order of the characters or treat it backwards.

    If you were to highlight the the statement above in a Workspace, <Operate-Click> and select Print it, Smalltalk would have displayed 'stop'.
    Hopefully, it's starting to make some more sense. Maybe not TOTAL sense, but at least some. How about one more, just for kicks? This one will really stretch your mind and test your understanding of objects and methods. If nothing else, it is an extremely interesting way of looking at something incredibly simple and explaining it a whole different way.

    3 + 4

    The 3 and the 4 are objects, instances of the SmallInteger class. So far, so good. Therefore, since every Smalltalk expression must contain at least an object and a message, the plus sign must be the message. Remember, in Smalltalk, every expression must eventually break down to the basic building block of Object - Method. This particular message is called a binary method because it involves 2 objects instead of one (that ought to make some sense, right?). If not, it will be further explained later. Since 3 is an instance of the SmallInteger class (it is called the receiver of the plus message), Smalltalk looks for the plus method for the SmallInteger class. It finds it. The plus method says that it expect a parameter to be passed (sent) to it. That would be the 4. The plus method is now happy and proceeds to execute the instructions it was designed to perform. It took the values of the 3 and 4 objects, added them together and returned another object, which in this case is the SmallInteger instance of 7.
    This may seem a bit odd. After all, most of your life, you were told that the plus sign was a mathematical operation. Other programming languages treat the plus sign as a command or built-in function. Now you are told that in Smalltalk, the plus sign is a message. Eventually, this will start to make more sense.

    The first 2 examples above represent the basic building block of the Smalltalk language. Just as in English, where the simplest grammatically-correct sentence contains just two words, a noun and a verb ("He jumped." or "She walks."), so too is a Smalltalk expression. It contains an object and a method (4 squared). These are called Unary Messages because they involve one object and one method. There are two other types of expressions in Smalltalk (which will be covered in a later primer) but those types will eventually break down to a unary message.

    Message versus Method. What's the Difference?

    If you re-read those explanations above carefully, you will notice a subtle interchangability between the words "message" and "method". Are they the same? Actually, they are not the same and it is very important that you understand why they are not the same. The best way to explain this is with another example.

    'pots' dirty

    Remember, the word "pots" to Smalltalk is not a "word" at all. To those who speak English, it is a word, but to Smalltalk, it is just a sequence of characters which Smalltalk calls a String. String is the class and "pots" is a particular instance of the String class.

    However, dirty does not happen to be a method that the String class understands. This is the difference - dirty is a message. The programmmer must think that Smalltalk knows what to do with a String object when given the message dirty. So the Smalltalk interpreter goes to the String class (because 'pots' is an instance of the String class) and asks, "Give me the dirty method for the String class." However, Smalltalk will not find a dirty method for the String class and will return a dialog box that says "Method Not Understood". So in this example, dirty was the message but it was not a method.
    In a real-life analogy, consider this. Walk into a night club where the guy at the piano is taking requests. Ask him to play "Far Far Away". He responds "I don't know that one." Ask him to play "Mack the Knife". He responds "OK" and then proceeds to play it. If the piano player (an instance of the NightClubPianoPlayer class) doesn't know a particular request (which is the message), he will come back and say "I don't know that one." This means that the request (message) is not part of his library of known songs (methods). If the piano player does know a particular request (message), he will come back and say "Here it is." and proceeds to execute the song. This means that the request (message) was part of his library of known songs (methods).

    Summary

    Hopefully, with the explanations and examples presented in this primer, you are beginning think in terms of objects. Keep these examples in mind when you return to the main tutorial lessons.