Object-Oriented Languages

views updated

Object-Oriented Languages

An object-oriented language is a computer programming language that revolves around the concept of an object. Object-oriented languages were developed to make it easier to develop, debug, reuse, and maintain software than is possible with earlier languages. Understanding objects, and object-oriented languages, requires knowledge of the evolution of computer programming languages and data structures.

Evolution of Computer Programming Languages

Computer programming languages have evolved continually over the years. This evolution is detailed in the following examples.

Assembly Language.

The first computer programs were written in assembly language. This is a primitive type of language in which each statement corresponds to a single machine instruction; it is the most basic computer operation possible. Accomplishing anything useful takes many machine instructions. Assembly language is specific to a particular type of computer; moving the program to a different type of computer requires writing a whole new program. Assembly language programs are difficult to write, debug, and maintain. Although other languages are now used for most computer applications, assembly language is still used today as the first language when a new chip is developed.

High Order Languages.

After assembly language, higher order languages were developed; among the early ones were FORTRAN and BASIC. One statement in a high order language corresponds to a sentence in English. A program called a compiler reads the statements from a source file and generates a file containing machine instructions, which is called an object file. The object file can then be loaded and executed by the computer. A high order language is more portable than an assembly language program; the same source file can be compiled for any computer as long as a suitable compiler exists.

Early high order languages only allowed for simple data types such as integer, floating point number, or string (a sequence of letters). The only data structure available was the array. An array is a list of elements that are all the same data type; for example, a list of numbers or a list of strings. A database was created using a group of arrays. For example, a product database might contain three arrays called Product Number, Product Description, and Product Price. It was up to the programmer to keep the arrays aligned; for example, to make sure the third element of each array corresponded to the same product.

Structured Languages.

The next step in the evolution of computer programming languages was the development of structured languages, like C and PASCAL, and the introduction of data structures. A data structure is an assembly of simpler data types into a single record. For example, a product database could be constructed as an array of product records, each record containing Product Number, Product Description, and Product Price fields. Now one record could contain all necessary information about a single item. Structures also became more defined in the procedural part of the language. A function or procedure is a small part of a larger program that could be written to provide some basic operation on a data structure such as a record.

Object Oriented Languages.

The next step in the evolution of computer programming languages, object orientation, was introduced in the Smalltalk language. Object orientation takes the concepts of structured programming one step further. Now, instead of data structures and separate program structures, both data and program elements are combined into one structure called an object. The object data elements are called attributes, while the object program elements are called methods. Collectively, attributes and methods are called the object's members. Usually, an object's methods are the only programs able to operate on the object's attributes.

With object orientation, a fundamental change came in the way programs are viewed. The earlier view was that data must be manipulated in some way to achieve a final result, and a program was viewed as a sequential means of performing the manipulations. From an object orientation perspective, a program is viewed as a group of objects that react to messages from the user, other programs, or other objects. This view led to the idea of event-driven programming; i.e. when event A happens, this object performs action B. A message is sent to an object by calling one of its methods.

Characteristics of Object-Oriented Programming

Key characteristics of object-oriented programming include encapsulation and data hiding, inheritance, and polymorphism.

Encapsulation and Data Hiding.

The central idea in object-oriented programming (OOP) is that the object attributes and the methods that operate on the attributes are bound together, or encapsulated, in the object. The object's methods provide the only interfaces between the object and other parts of the program. This is different from earlier languages, where any part of a program could operate on any piece of data at any time. Although this seems restrictive, the restrictions result in a more modular program that is easier to develop and less likely to contain errors. It also means it is easier to move an object to a different environment and still have it function correctly.

A software object is somewhat similar to a physical object. For example, an engine can be used to power a car. It has internal components, corresponding to attributes, but one does not have to be concerned with what they are or how they work. The engine must interface with the throttle, fuel system, transmission, intake, and exhaust manifolds, all of which correspond to methods. It is inconceivable that fuel would get into the engine by any means other than by way of the fuel system. As long as the correct interfaces are maintained, the engine will work. So it is with objects. The object attributes are hidden from the outside. The object interacts with its environment through its methods.

Inheritance.

Another important concept to object-oriented programming is inheritance. An object class is defined in a hierarchy, and is said to inherit the behavior of its ancestors (those objects above it in the hierarchy). For example, a drawing program might include three object classes: Shape, Rectangle, and Circle. They could be defined so that Rectangle and Circle are both descendents of Shape.

Shape includes attributes common to any shape, such as the location of the shape on a drawing surface. Shape also provides methods for manipulating those attributes. For example, a move method would change the shape's location. Additionally, it would provide a definition for methods to which all shapes must be able to respond, for example, a draw method to display the shape on a drawing surface. The draw method in this case is said to be abstract; it does not do anything other than create a requirement that descendent classes must implement it.

Since Rectangle is a descendent of Shape, it inherits attributes (location) and methods (move) from Shape. It provides the additional attributes it needs (width and height) and new methods that manipulate those attributes (setWidth, setHeight). Rectangle must also provide a draw method that paints a rectangle on the drawing surface, because every descendent of Shape must implement a draw method. Likewise, Circle provides a new attribute (radius), methods for manipulating it (setRadius), and a draw method of its own.

With this kind of arrangement, the drawing manager program would have a list of shapes on the drawing surface. To move an object, it would call the object's move method. To draw an object, the manager calls the object's draw method. The manager neither knows nor cares how the object moves or draws itself, as long as the job gets done. In fact, it may not even know what kind of Shape a particular object really is. It could be a Rectangle, a Circle, or any other object descendent from Shape. It only needs to know that it is descendent from Shape, so it can send to it any message that a Shape can receive.

The inheritance capability of an object-oriented language added a whole new dimension to programming. Learning an earlier high order language was mainly involved with learning the language syntax (how the language statements are constructed), which is not that difficult. In an object-oriented language, learning the syntax is still necessary, but becoming familiar with the standard class hierarchieswhich may include thousands of classes, each class with its own methodsis a much larger task. It is worthwhile, however, because an object inherits the attributes and behavior of its parent. A programmer can avoid unnecessary work by finding an existing object that already does most of what is needed. Then new capability can be added incrementally. The result is lower cost, higher quality software.

This characteristic also led to the inclusion of automatic documentation features in several object-oriented languages. In earlier languages, documentationif generated at allwas done separately, almost as an afterthought. Now documentation information can be included in the object source code and used to generate a Hypertext Markup Language (HTML) document automatically, complete with hyperlinks up and down the class hierarchies, which may be viewed with an Internet browser. This makes it much easier to maintain accurate, up-to-date documentation.

Polymorphism.

The next important characteristic of object-oriented language is polymorphism, which means that a descendent object does not have to respond to a message exactly like its ancestor does. A new object can override its parent's methods, which causes the new object to react to a message differently. For example, a common class in a windowing system is a Component, which represents a visible object. A Component is an ancestor class for every visible object on the screen: icons, buttons, menus, slide bars, check boxes, radio buttons, even windows. All of these descendent classes override some of Component's methods to change behavior. For example, an Icon object needs to display itself as a small picture. Icon overrides Component's draw method to show the picture.

Common Object-Oriented Languages

Common object-oriented languages include Smalltalk, C, Java, and other languages such as BASIC and PASCAL.

Smalltalk.

Smalltalk was the original object-oriented language, developed in the early 1970s by Xerox. Since then, several variations have been introduced. It is still being used, but widespread acceptance has been hampered by the lack of a universal standard.

C+ +.

C+ + is an extension of the C language that provided OOP capabilities. It is probably the most widespread object-oriented language currently in use. C is a hybrid language because it compiles standard C programs; it does not require the use of objects. This enables it to take advantage of existing C software, while using object orientation for new software. C is controlled by ANSI standards.

Java.

Java is the most widely accepted pure object-oriented language. It was developed by Sun Microsystems originally as a control language for small appliances. However, it proved ideal for use with the Internet. A Java applet can be embedded in a web page. When a browser loads the web page, it also loads and displays the applet. Sun still maintains strict control of the language standard.

To facilitate cross-platform operability (working on entirely different computer types without recompiling), Java is implemented in two parts. The compiler produces an object file that can only be executed by a Java Virtual Machine (JVM). A separate JVM is available for each supported operating system (Windows, Unix/Linux, or Solaris). This makes Java programs able to run on any of those systems without recompiling.

Other Languages.

Most languages commonly used today allow some form of object orientation. BASIC has evolved into object-oriented Visual BASIC; PASCAL into object-oriented DELPHI. Generally these are hybrid languages, like C, that support object orientation without requiring it.

see also Compilers; Mouse; Procedural Languages.

Donald M. McIver

Bibliography

Deitel, Harvey M., and Paul J. Deitel. Java: How to Program, 2nd ed. Upper Saddle River, NJ: Prentice Hall, 1998.

Voss, Greg. Object-Oriented Programming, An Introduction. New York: Osborne McGraw-Hill, 1991.