Chapter 2
The Essence of Objects
So, what exactly is Object Orientation? It is a problem solving technique
used to develop software systems. Object orientation is the culmination of
years of experience in finding an effective way to develop software, and is
certainly the dominant method used to develop major software systems today.
Object orientation is a technique of
modeling a real-world system in software based on objects. The object is the
core concept. An object is a software model of a real-world entity or concept. Almost
anything can be modeled as an object in software. For example, you could model
a temperature sensor as an object. Or, in a more abstract system, you could
model color as an object. Even something as basic as a number can be considered
an object that has a value and a type. Typically, each object has an associated
set of attributes such as value, state, or whatever else is needed to model the
object. Sensor attributes might include a state such as active or inactive, an
attribute indicating its current value, and information about its physical
location. Objects usually provide the ability to modify their state as well. In
addition to keeping track of the current temperature, for example, a
temperature sensor object might provide
a way to turn the sensor on or off. The
attributes, or data, used inside an object are really only a tiny part of what an object is and does. An object also has a
set of. responsibilities that it carries out by providing services to other
While
a program is running, individual objects usually don't stand alone. They belong
to a collection of other similar objects that all are members of the same
group, or class. A program will be made up of many different classes, each
class made up of similar objects.
class A class is a
description of a set of objects. The set of objects share common attributes and
common behavior. Class is similar in concept to abstract data types found in
non- OO programming languages, but is more comprehensive in that it includes
both structure and behavior. A class definition describes all the attributes of
member objects of that class, as well as the class methods that implement the
behavior of member objects.
object The
basic unit of object orientation. An object is an entity that has attributes,
behavior, and identity. Objects are members of a class, and the attributes and
behavior of an object are defined by the class definition.
Classes
and objects are closely related, but are not the same thing. A class is a
description or definition of the characteristics of objects that belong to that
class. An object is a single instance or member of a class that is created and
exists while the program is running. A class may have just a single object or
any number of member objects existing at any given time. All members of a class
have similar behavior. For example, consider a software system used to monitor
various sensors in a factory. One obvious kind of object present is such a system
is a sensor. A class called Sensor would be defined and used to model physical
sensors. The class would define the basic characteristics of any Sensor, such
as its location, value, and identification number, as well as a set of services
used to carry out its responsibilities.
What Is an Object-Oriented System?
Just what is an object-oriented system? What makes an OO system different
than other software systems? One way to define an objectoriented system is to
use a list of properties that characterize objectoriented systems. A
non-object-oriented system might share some properties such as using
abstraction or encapsulation, but will not be built using objects or classes.
It is also possible to use an objectoriented language to implement a system
using classes or objects, but the system must have all the following properties
to be considered a true object-oriented system.
Any object-oriented software system will have the following
properties:
1. Abstraction with objects
2. Encapsulated classes
3. Communication via messages
4. Object lifetime
5. Class hierarchies
6. Polymorphism
The next section gives a brief overview of each of these
properties, while the following sections give more detailed explanations of
each.
object orientation A method of developing software that uses abstraction with
objects, encapsulated classes, communication via messages, object lifetime,
class hierarchies, and polymorphism.
Fundamental Properties of an Object-Oriented System
Abstraction with objects
An abstraction is a mechanism that allows a complex, real-world situation
to be represented using a simplified model. Object orientation abstracts the
real world based on objects and their interactions with other objects. For
example, one possible abstraction of a color is the RGB model.
abstraction A model of a real-world object or concept.
Encapsulated classes
The
abstractions of related ideas are encapsulated into a single unit. The states
and behaviors of the abstraction are incorporated into an encapsulated whole,
called a class. The actual internal implementation of the states and behaviors
is hidden from the rest of the system. While this not a new programming
technique, in OO the encapsulation is an inherent and integral part of the
system and design. Earlier, we described a Color class that provided a way to change
it red, green, or blue values. In fact, as long as the outside world continues
to see and use a Color object in a consistent way, it wouldn't matter just how
color is represented internally by the Color object. It could use either the HSV
(hue, saturation, value) color model or the RGB model internally, and the
outside world would be unaffected. The state and behavior of objects are
controlled by welldefined and restricted interfaces to the object.
Encapsulation ensures that the internal details of an object are hidden from
the rest of the world, that each object maintains its own unique identity and
state, and that the state can only be changed by well-defined messages.
encapsulation The
process of hiding all the internal details of an object from the outside world.
In Java, encapsulation is enforced by having the definitions for attributes and
methods inside a class definition.
Interaction via messages
In order to accomplish useful tasks, objects need to interact with
other objects. The interaction can be between objects of the same class, or
objects of different classes. This interaction is handled by sending messages
(in Java, this is done by calling methods) to other objects to pass information
or request action. For example, when a user selects a command button in a
dialog box on the screen by clicking the mouse, a message is sent to the dialog
object notifying it that the command button was pressed. Messages can be
used to change the state of an object or to
request an action by the object.
Object lifetime
All objects have a lifetime. They are created and initialized as
they are needed during program execution, exist and carry out their functions,
and are eventually destroyed. While objects exist, they maintain their own
identity and state. Many objects that are instances of the same class can exist
at any given time. Each object has a unique identity, and will have attributes
that are different from other instances of objects in the same class.
Class hierarchies
In an OO design, classes of objects are arranged into hierarchies
that model and describe relationships among the classes. The simplest relationship
is an association. For example, there could be an employment association
between a person and a company. These simple associations exist between
different classes.
A second way to use a hierarchy is to define more specialized
classes based on a pre-existing generalized class. For example, a dialog class can
be considered a specialized case of a more general window class. The more
specialized class will automatically share the attributes of the more general
class (e.g., size and screen position), and will probably add new
attributes and behaviors to the generalized class (e.g.,
associated control objects). This kind of hierarchy is called inheritance.
Polymorphism
Polymorphism is the final fundamental characteristic of
objectoriented systems. When inheritance is used to extend a generalized class
to a more specialized class, it will usually include extending some of the behaviors
of the generalized class. The specialized class will often implement a behavior
that is somewhat different than the generalized class, but the name used to
define the behavior will be the same. It is important that a given instance of
an object use the correct behavior, and the property of polymorphism allows
this to happen automatically and seamlessly. Polymorphism is actually easier to
use than it is to explain.
Abstraction with Objects
Abstraction is one of the basic tools of all programming, not just
OO programming. When trying to write a program to solve some real world
problem, abstraction serves as a way model the real world problem. For example,
if you were trying to write an address book program, you would use abstractions
such as names, addresses, phone numbers, alphabetical order, and other
concepts associated with an address book.
Encapsulated Classes
Encapsulation is one of the most important aspects of OO. It is
what allows each object to be independent. The exact implementation of attributes
and of object behavior is hidden from the rest of the world through
encapsulation. The class definition is the main programming tool that is used
to implement encapsulation. A class is a description of a collection of objects
with common attributes, behavior, and responsibilities. The definition or
specification of a class includes the definitions of the attributes
comprising the state, the methods that carry out the responsibilities
of the class by implementing the behavior, and how to
set the initial attribute state of an object. A class is identified by a name.
As
long as you maintain a well-defined interface to the rest of the world, you can
easily modify your class definition without breaking the rest of the system.
attribute Used to hold
state information of an object. An attribute might be as simple as an on or off
boolean variable, or it might be a complex structure such as another object. A class
definition describes the attributes and operations (methods) of a class.
behavior The activity
of an object that is visible to the outside world. Includes how an object
responds to messages by changing its internal state or returning state
information to other objects.
method
An
operation or service performed upon an object, defined as part of the
declaration of a class. Methods are used to implement object behavior. Synonyms
for method include member function, operation, and service.
state State reflects
the current values of all the attributes of a given object, and is the result
of the behavior of an object over time.
Java
programs are defined as collections of classes. Normally each Java class is
defined in a separate file. The attributes of a class are defined by the
declaration of variables of various types such as int or boolean. A Java class
includes the definitions of the methods used to implement the behaviors of the
class. The method definitions are integral parts of the class definition.
Communication via messages
Messages are how objects communicate with each other. Any object may
send a message to other objects, and it may receive messages from other
objects. In practical programming terms, sending a message is accomplished by
invoking or calling some class method, and receiving a message is accomplished
by having a class method called by a different object. Usually, a message is
sent by a method call as a normal part of the execution of the program logic of
an object. However, messages may also originate from the operating system
interface or language runtime system. Consider an object that implements a
dialog interface with a user. When the user clicks on a button, a message is
sent to the dialog object or button handler telling it that a specific button
has been pressed (the implementation specifics aren't important).
Object
orientation is a natural for this kind of programming. In an OO program, what
you often end up with is a set of objects that can respond to a set of messages
originating from a variety of sources such as a mouse click, a sensor value
change, or a data base transaction. Individual encapsulated objects can respond
to messages and send their own messages to others objects in response. Objects
in the system interact via well-defined messages with other objects in the
system.
Object Lifetime
Objects
are dynamic entities. They are created on an as-needed basis during program
execution. When an object is created, it is instantiated, or bound to the class
definition. An instantiated member of a class is called an object, or
equivalently an instance. When a new object first comes into existence, a
special method called the constructor is automatically invoked by the run-time
system. The constructor is responsible for giving an instance its initial
state. Once an object has been created, it can receive and send messages. To
get an idea of object lifetime, consider graphical user interface (GUI) classes
such as the Swing library provided with Java. One type of object included in a
GUI is a dialog interface. Upon some action by the user, say selecting a menu
item, a given dialog object will be created. As part of the creation process,
its constructor will be called.
When
the user closes the dialog, the dialog object will no longer be needed. Once it
no longer has any references to it, it can be garbage collected by the Java
run-time.
constructor
An
operation that creates an object and defines its initial state. For complex
objects, construction can be a significant activity, and cause the constructors
of other objects to be invoked as well.
garbage collection The automatic
detection and freeing of memory that is no longer in use. An object becomes
available for garbage collection when it is no longer referred to by any other
object. Java uses garbage collection rather than explicit destructors found in
other OO languages such as C++.
reference A data element
whose value is an address. In Java, all objects are accessed by reference. Any
object variable will be a reference to an actual object, and not the object
itself.
identity The
characteristics or state of an object that allows it to be distinguished from
other objects.
instance A specific
object that is an instantiation of a class. An instance has specific attributes
and behaviors, and a unique identity. Instance and object are often used
interchangeably.
instantiation Creating
an instance of an object of a given class. Instantiating an instance brings it
into existence.
object lifetime The
time an object exists - from its instantiation in its constructor until the
object no longer exists and has been finalized by the Java garbage collector.
The creation point of an object is under program control, but the exact moment when
an object ceases to exist cannot be determined because of the way the Java
garbage collector works. Basic UML Class Notation The basic UML notation for a
class is a rectangle with three horizontal parts. The top part is used to hold
the name of the class. Associations are
shown by lines between classes, and are usually labeled with the name of the
association. Inheritance is shown by a line with a triangular arrow pointing to
the more general class (the superclass). Aggregation is shown by a line with a
hollow diamond pointing to the whole class, while composition uses a solid
diamond instead.
Figure 2-4.
An animal
Generalization/Specialization Hierarchy
Note that the UML uses an open arrowhead
pointing to the more general class to show inheritance. Thus, in Figure 2-4 of
the animal hierarchy, a Mammal inherits from Animal, and so on.
UML: You can show more or less detail in UML
diagrams depending which information you want to concentrate on. For example,
since Figure 2-4 is mainly focused on the inheritance relationships, and it
doesn't show the attribute or operation boxes. We will use this same diagram in
Figure 2-5, but show more detail, including some selected operations.
Many
discussions of object orientation will give special emphasis to programming
with inheritance, but in fact, both aggregation and inheritance are important
parts of object-oriented programming. Both kinds of hierarchies are used to
design programs that reflect the characteristics of the problem being modeled.
Any class can be defined using a combination of both kinds of hierarchy.
default behaviors In an
inheritance hierarchy, the class behaviors defined by superclasses that will be
used by default unless they are overridden by some subclass.
derived In an
inheritance hierarchy, a subclass is derived from a superclass. The derived
subclass inherits the attributes and methods of the parent superclass.
generalization/specialization An
inheritance hierarchy. Each subclass is a specialization of a more generalized
superclass.
implements In Java, a
specification that the class will implement the code required by an interface.
inheritance A mechanism that
allows one class (subclass) to share the attributes and behaviors of another
class (superclass). Inheritance defines an is-a relationship between classes.
The subclass or derived class inherits the attributes and behaviors of the
superclass, and will usually extend or modify those attributes and behaviors.
interface In Java, an
interface is a specification of methods a class using the interface must
implement. An interface is a specification, and does not define any code. It
provides an alternative to multiple inheritance.
is-a A term used in
inheritance hierarchies. In general, a
subclass is-a specialized kind of a more
general superclass.
is-a test A simple test to
check for proper inheritance design. If
you cannot say a subclass is-a kind of the
superclass, then
inheritance is probably not appropriate.
multiple inheritance When
a subclass is derived from multiple superclasses, it is said to have single
inheritance. Java does not allow multiple inheritance, but provides interfaces
as an alternative.
overriding When a subclass
specifies an alternative definition for an attribute or method of its
superclass, it is overriding the definition in the superclass. Also called
overloading. Java can only overload methods.
root class The top most or
most generalized user class of an inheritance hierarchy. In Java, all classes
are at least implicitly derived from the Java Object class, which make it the
most primitive root class. Most applications will have many hierarchies with
different non-Object root classes.
single inheritance When
a subclass is derived from a single superclass, it is said to have single
inheritance.
subclass In an
inheritance hierarchy, a subclass is derived from an associated superclass. A
subclass is a specialization of the generalized superclass.
superclass In an
inheritance hierarchy, a superclass is a more generalized class. A subclass
will be derived from a superclass. (A superclass is also known as a parent
class or a base class.)