Introduction to Object Technology

In computer programming, objects, or more precisely, the classes used to construct objects, are intended to be reusable software components.

There are string objects, output objects, input objects, graphics objects, date objects, time objects, audio objects, video objects, etc.

Almost any noun can be reasonably represented as a software object in terms of attributes (e.g., name, color and size) and behaviors (e.g., calculating, moving and communicating).

Using a modular, object-oriented design and implementation approach can make software-development more productive. Object-oriented programs are often easier to understand, correct and modify.

An Object-Oriented Car Analogy

In the real-world, there are many analogies of objects. Let us take a car as an example. A car is an object that can be used to transport people and goods. A car has an engine, four wheels, a number of doors, and a lot of other mechanical and electrical parts. These parts are hidden under the hood. A driver controls the car using a steering wheel, the gas and brake pedals, and many buttons and switches on the dashboard. If you are driving a car and want to make it go faster, you press its gas pedal with your foot.

The specific car you drive is one of many cars of a particular model that were made by a car factory according to a specific design or blueprint. The blueprint of the car may contain a set of engineering drawings and documents that specify the details of building the car. Specifically, the blueprint will include the steering wheel, gas and brake pedals, etc.

The gas pedal hides from the driver, or the user of the car, the complex mechanisms that actually make the car go faster, the brake pedal hides the mechanisms that slow the car, and the steering wheel hides the mechanisms that turn the car.

This enables people with little or no knowledge of how engines, braking and steering mechanisms work to drive a car easily and safely.

To be able to use a car to travel, the car has to be made by the factory.

In computer software, an object is used to keep and modify information. It is also made according to a blueprint and used by a user (which is typically another program). In the following, we explain a set of object-oriented programming concepts by drawing analogy to a car.

Methods and Classes

In Java, a class is a blueprint of a set of objects. It describes how the object is made of and what tasks the object is able to perform.

A method performs a task by a Java object. The method contains a sequence of Java statements that actually perform the task.

A method hides these statements from its user, just as the gas pedal of a car hides from the driver the mechanisms of making the car go faster.

In Java, a class is a programming module containing the methods that perform the class's tasks. Thus, the class is similar in concept to a car's design, which specifies the "methods" to accelerate, steer, brake, and so on.

Instantiation

Just as a factory has to construct a car from its blueprint before you can actually drive a car, a Java program must ask the Java compiler to construct an object of a class before it can make method calls to the object. This is called the instantiation of an object, and hence an object is referred to as an instance of its class. Technically, the instantiation of a Java object will allocate memory for the object to hold information and identify code sections that implement the methods.

Reuse

Just as a car's blueprint can be reused many times to build many cars, a Java class can be reused many times to build many objects.

Reusable classes save programming time and effort and helps you build more reliable and effective systems. This is because existing classes often have gone through extensive testing, debugging and performance tuning.

Just as the notion of interchangeable parts was crucial to the Industrial Revolution, reusable classes are crucial to the software revolution that has been spurred by object technology.

The reuse of a class is not limited to the original design. A class can be extended to add new constructs or modified to perform new tasks.

Method Calls

When you drive a car, pressing its gas pedal sends a message to the car and makes it to perform a task, namely, to go faster.

Similarly, when using a Java object, you make method calls to an object. A method call sends a message to the object to perform a task by executing the method's statements. The message may include additional information that is needed to perform the task.

Attributes and Instance Variables

A car has many attributes, such as color, its number of doors, the amount of gas in its tank, its current speed and its record of total miles driven (i.e., its odometer reading), etc. These include the physical parts as well as the information about the parts.

The car's attributes are specified in its blueprint. Every car has its own attributes. Each car knows how much gas is in its own gas tank, but not how much is in the tanks of other cars.

A Java object also has attributes. These attributes are information items that the object keeps while it is used in a program. The object keeps these attributes in its assigned memory which is organized like a C struct. Attributes are specified in the class of the object as instance variables or fields.

Each object knows its own attributes, but not the attributes of other objects.

Encapsulation

Just as a car hides its parts under the hood, an object encapsulates (i.e., hides) its fields and the statements of its methods.

Ideally, objects are not allowed to directly modify or access each other's fields except through method calls. The implementation details should be hidden within the objects themselves. Information hiding is crucial to good software engineering.

Inheritance

A new class of objects can be programmed quickly and conveniently by inheritance, that is, extending existing classes. The new class created in this way will have the fields and methods of an existing class, possibly customizing them and adding unique ones of its own.

In our car analogy, a convertible car of a model can be an modification of the model so that the roof can be raised or lowered, but the other parts are the same. So the blueprint of the convertible is an extension of the more general blueprint of that model.

Polymorphism

Often, a method does not apply just to a specific class of objects, but to a more general class. For example, a speed limit usually applies to all vehicles on the road, whether they are cars, trucks, motorcycles, etc. Thus, drivers of convertibles can determine whether they are driving within the speed limit because a convertible is a type of car, which is a type of vehicle.

Code that is written for a general class is polymorphic and will usually depend on methods that are common to all the specific classes. In Java, an interface is often used to specify the names, parameters and return types of these methods.

Object-Oriented Analysis and Design

How will you create the code for your programs?

You should follow a detailed analysis process for determining your project's requirements (i.e., defining what the system is supposed to do).

You should develop a design that satisfies them (i.e., deciding how the system should do it).

Object-oriented analysis and design (OOAD) views a system as a group of interacting objects. Object-oriented programming (OOP) allows you to implement an object-oriented design as a working system. It provides a compiler that allows a programmer to define classes. The Unified Modeling Language (UML) is the most widely used graphical scheme for OOAD.