Starting from Scratch: How Do You Communicate in Object-Oriented Programming?
In the following article, Jeremy Jordan, the Marketing Manager for Cincom Smalltalk, shares more about his first experience with object-oriented programming:
“Recently, I had an opportunity to try my hand at programming in Smalltalk. I’ve never programmed in my life, so this opportunity was both exciting and intimidating. Thankfully, one of our Cincom Smalltalk engineers recommended that I read the ‘Smalltalk User Guide’ found in Cincom ObjectStudio, which was a game-changer for me. After reading through some of this document, I finally understood what object-oriented programming represented, at least from a high level. Although this may be second nature to seasoned Smalltalkers, I would like to share another segment of snippets that I learned.”
The topics that follow provide a brief overview of the object-oriented principles that underlie Smalltalk programming. This section is all about communication, as we learn the definition of an object and how objects communicate. You will see that objects are used to model real-world entities, and that they have behavior and responsibilities.
An object is a combination of:
- Some data elements
- Operations that manipulate the data and/or perform other actions
In other contexts, these data elements are called “fields” or “attributes.” In Smalltalk, the data elements are called “instance variables.” There are also other kinds of variables such as class variables, global variables and temporary variables.
Operations that manipulate the data and/or perform other actions are the interface or behavior of the object. In Smalltalk, each individual procedure is called a method.
An object’s data elements are private to the object, and you cannot access them directly from outside the object. Instead, you must send a message to the object, asking for information. It is as if there is a wall between you and the data—you cannot see the data directly, but can only gain information by talking to the object and getting a response. For example, to determine the height of the man, you cannot just look at him (or read his mind); you must ask him: “What is your height?”
This is called sending a message to the man. In Smalltalk, all programs are written by sending messages to objects. If the object understands the message, it will perform one of its methods and respond with an answer. If the object does not understand the message, it will tell you so. In Smalltalk, you write the pictured message as:
aMan is the object and whatIsYourHeight is the message.
Notice that the message, whatIsYourHeight, is passed to the object, aMan. In more traditional languages, you pass data to functions.
The only access to an object’s data elements is through messages. This is a very important object-oriented principle called “encapsulation.” Encapsulation is useful because it decouples the behavior of the object from its internal implementation.
In the preceding object example, you do not care how the man calculates his height. He may know his height immediately. Or, he may have to measure himself each time. Or, he may have to ask his mother. The point of encapsulation is that you should not need to find out which method he uses.
Here are some other examples:
- DatabaseTable object would have insert, update and delete messages as its interface, but the internal representation could be anything we want—a tree, a flat file, a hash table and so on.
- A Float object. Do you know or care what the internal implementation of a float is? Or the implementation difference between an 8-byte float and a 4-byte float? Probably not. All you care about is the behavior of the float, not its implementation.
This separation of the interface from implementation greatly improves the reusability and maintainability of the design. Indeed, the encapsulation principle is also stressed strongly in structured programming.
With object-oriented programming, users are learning a programming language and a new way of thinking. When learning a new language of any kind, Linguist.com says, “You need to experience a language through lots of exposure before you can hope to learn it.” The same is true for object-oriented programming. The best way to learn OOP is to learn by immersing yourself in an environment, like Smalltalk, where you use objects all of the time.