You are on page 1of 5

1

Introduction

Object-oriented programming has been in practice for many years now. While the fundamental object-oriented concepts were first introduced via the class construct in the Simula programming language in the 1960s, the programming technique was only accepted with the advent of Smalltalk-80 more than a decade later. Object-oriented programming has come a long way. More and more programs are now designed and developed using this approach. What is object-oriented programming? What makes it attractive as an alternative programming approach? How does it differ from the traditional procedural programming approach? These questions will be discussed in this chapter.

1.1

Object-Oriented Programming

The procedural approach to programming was the de facto approach in the early days of programming. Here, code is modularized based on a systems processes. For instance, in developing a library application system, we would have considered processes such as the checking in an d out of books, making reservations of books, cataloging of books, and so on. Problem solving would involve the analysis of these processes in terms of the procedural tasks carried out and the production of a system whose representation is based on the procedural flow of the processes. Object-oriented programming, on the other hand, models objects and their interactions in the problem space and the production of a system based on these objects and their interactions. Since the real-world problem domain is characterized by objects and their interactions, a software application developed using the object-oriented programming approach will result in the production of a computer system that has a closer representation of the real-world problem domain than would be the case if the procedural programming approach is used.

Object-Oriented Programming and Java

1.2

Objects and Their Interactions in the Real World

Let us consider a real-world situation. There are two persons, Benjamin and his wife, Bambie. They are customers of HomeCare, a company dealing in luxurious furniture. HomeCare sells a variety of sofa sets. Each sofa set is labeled with an identification number and a price tag. After viewing the sofa sets for an hour, Benjamin and Bambie decide to purchase a green leather 5-seater set. They approach Sean, a salesperson, to place their order. In making his request known to Sean, Benjamin sends a message to Sean, I would like to purchase this green leather, 5-seater set. Can you please have it sent to me by next Wednesday? The message that Benjamin has sent to Sean is a takeOrder message. It contains information such as the type of sofa set (a green leather, 5-seater set) and the date of delivery (next Wednesday). This information is known as the parameters of the takeOrder message. In response to Benjamins message, Sean replies to Benjamin by returning the result of his request. We can represent the interaction between Benjamin and Sean graphically using Figure 1-1.

result

takeOrder Benjamin Sean

Figure 1-1: Interaction between Benjamin and Sean.

Sean was able to respond to Benjamins takeOrder message because he understood it and had the means to handle Benjamins request. Although Sean knew how to satisfy Benjamins request, Benjamin did not. In fact, most of the time, customers do not know how a salesperson has satisfied their orders. All they get from salespersons are replies such as, I am sorry, madam, we are unable to satisfy your request because the sofa you wanted has been sold, or Sir, your request has been satisfied. We will deliver the goods on Wednesday between 10 am to 11 am to the address indicated. Thank you for your order. Sean, as a salesperson at HomeCare, has a responsibility towards Benjamin. He maintains his responsibility by applying a set of operations: 1. 2. 3. 4. He determines if there is sufficient stock to satisfy Benjamins request. He determines if the requested date for delivery is a suitable date. He instructs the warehouse staff to deliver the goods to Benjamins address on the requested date, if the above conditions are satisfied. Finally, he informs Benjamin the result of his request.

Introduction

1.3

Objects and Their Interactions in Programming

The interactions between Benjamin and Sean in the above real-world situation can be represented in object-oriented programming terms. For instance, Benjamin and Sean are objects that interact by sending messages. Benjamin is thus a message-sending object, while Sean is a message-receiving object. Alternatively, we can label Benjamin as a sender and Sean as a receiver. The takeOrder request from Benjamin to Sean is an example of a message. It may have additional, accompanying information known as parameters (or arguments) of the message. The fact that Sean responded to Benjamins message indicates that the message is a valid message. Each valid message corresponds to a method that Sean uses to fulfill his responsibility to Benjamin. An invalid message, on the other hand, is one that the receiver does not have the capability to respond to, that is, the receiver does not have a corresponding method to match the message. For example, if Benjamin had requested a discount on the price, his request would have been rejected because Sean, being a salesperson, would not have the capability (or a corresponding method) to respond to the message. A method contains a number of operations detailing how Sean is to satisfy the demand Benjamin put on Sean through the request. Figure 1-2 summarizes the relationships of these terms.
Message Arguments Method takeOrder(sofa type, who, address, delivery date)

Benjamin

result

Sean

Object

Object

Figure 1-2: Object interactions in object-oriented programming terms.

While Benjamin may know what Sean can do through his methods, he may not know how Sean does them. This is an important principle of object-oriented programming known as information hiding: the sender of a message does not know how a receiver is going to satisfy the request in the message.

1.4

Simulation

Based on the above example, it is clear that concepts in object-oriented programming such as object, message and method, do provide a close representation of real-world objects and their interactions. These concepts are thus suitable for simulating actual object interactions in real-world situations.

Object-Oriented Programming and Java

It is this ability for modeling real-world problems that identified object-oriented programming as being suitable for simulation. The Simula programming language was designed in the early 1970s to provide simulation abilities using object-oriented concepts.

1.5

Java

Java was first introduced in 1995 as a simple and secure object-oriented programming language. It is a unique language in that, being a new language at that time, it was able to attract a lot of interest from the computing community. Within two years after Java was launched, there were an estimated 400,000 Java programmers and over 100 books on Java programming. There are a few possible reasons for the phenomenal interest in Java. The year 1995 saw a maturing of Web technologies, and Javas multiplatform capability, which enabled a Java program to execute on any computer, was exceedingly attractive, especially on an open network like the Internet. Java is implemented via part compilation and subsequent execution on an interpreter implemented in software. Java applications are therefore object code portable as long as a Java virtual machine is implemented for the target machine. The popularity of Java is also ironically due to its similarity with its close rival C++. Java takes the pain out of learning a new language by reusing much of C and C++. At the same time, safe programming practice in Java and language facilities for automatic memory management were benefits that were attractive to programmers on the verge of deserting their C/C++ camps. In relation to the Internet, Java applets have given rise to a new generation of distributed applications with low software distribution and maintenance costs. As applets are embedded in an HTML document via <APPLET> tags, its transmission to the client machine for execution is implicitly handled by the underlying network protocols and thus makes the typical channels of distribution and installation obsolete. While the object-oriented programming framework promotes reusability of software and code, this very practice has been demonstrated in the rich set of class libraries seen in the Java language. The Java foundation class libraries provide for windowing and graphical user interface programming, network communications, and multimedia facilities. Together, they demonstrate the practical and productive work done in Java.

1.6

Summary
In this chapter, we discussed: x An overview of object-oriented programming concepts and their applicability for modeling and representing real-world entities and their interactions in the problem-solving process.

Introduction

x Object-oriented concepts of object, message, and method. x An overview of the Java programming language and the potential of productive software development

1.7

Exercises
1. Distinguish the programming approach used in procedural programming and object-oriented programming. 2. Discuss the validity of the following statement: The object-oriented programming approach is ideal for simulating real-world problems. 3. Consider the following scenarios and outline the objects and their interactions in terms of messages and arguments: (a) a driver driving a car; (b) a customer making a cash withdrawal from an automated teller machine (ATM); (c) a customer buying a compact disk player from a vendor; (d) a traffic policeman directing traffic at a junction; (e) a lecturer delivering his/her lecture to a class of students; (f) a tutorial discussion between an instructor and students.

You might also like