What Exactly Is an Object?

 <  Day Day Up  >  

Objects are the building blocks of an OO program. A program that uses OO technology is basically a collection of objects. To illustrate , let's consider that a corporate system contains objects that represent employees of that company. Each of these objects is made up of the data and behavior described in the following sections.

Object Data

The data stored within an object represents the state of the object. In OO programming terminology, this data is called attributes . In our example, as shown in Figure 1.6, employee attributes could be Social Security numbers , date of birth, gender, phone number, and so on. The attributes contain the information that differentiates between the various objects, in this case the employees. Attributes are covered in more detail later in this chapter in the discussion on classes.

Figure 1.6. Employee attributes.

graphics/01fig06.gif

Object Behaviors

The behavior of an object is what the object can do. In procedural languages the behavior is defined by procedures, functions, and subroutines. In OO programming terminology these behaviors are contained in methods , and you invoke a method by sending a message to it. In our employee example, consider that one of the behaviors required of an employee object is to set and return the values of the various attributes. Thus, each attribute would have corresponding methods, such as setGender() and getGender() . In this case, when another object needs this information, it can send a message to an employee object and ask it what its gender is.

Getters and Setters

The concept of getters and setters supports the concept of data hiding. Because other objects should not directly manipulate data within another object, the getters and setters provide controlled access to an object's data. Getters and setters are sometimes called accessor methods and mutator methods, respectively.


Note that we are only showing the interface of the methods, and not the implementation. The following information is all the user needs to know to effectively use the methods:

  • The name of the method

  • The parameters passed to the method

  • The return type of the method

To further illustrate behaviors, consider Figure 1.7.

Figure 1.7. Employee behaviors.

graphics/01fig07.gif

In Figure 1.7, the Payroll object contains a method called CalculatePay() that calculates the pay for a specific employee. Among other information, the Payroll object must obtain the Social Security number of this employee. To get this information, the payroll object must send a message to the Employee object (in this case, the getSocialSecurityNumber() method). Basically, this means that the Payroll object calls the getSocialSecurityNumber() method of the Employee object. The employee object recognizes the message and returns the requested information.

To illustrate further, Figure 1.8 is a class diagram representing the Employee / Payroll system we have been talking about.

Figure 1.8. Employee and payroll class diagrams.

graphics/01fig08.gif

UML Class Diagrams

Because this is the first class diagram we have seen, it is very basic and lacks some of the constructs (such as constructors) that a proper class should contain. Fear not ”we will discuss class diagrams and constructors in more detail in Chapter 3, "Advanced Object-Oriented Concepts."


Each class diagram is broken up into two separate sections (besides the name itself). The first section contains the data (attributes), and the second section contains the behaviors (methods). In Figure 1.8, the Employee class diagram's attribute section contains SocialSecurityNumber , Gender and DateofBirth , while the method section contains the methods that operate on these attributes. You can use programming tools such as Rational Rose or TogetherJ to create and maintain class diagrams that correspond to real code.

Modeling Tools

Rational Rose and TogetherJ are visual modeling tools that provide a mechanism to create and manipulate class diagrams using the Unified Modeling Language (UML). UML is discussed throughout this book, and you can find a description of this notation in Chapter 10, "Object Modeling with UML."


We will get into the relationships between classes and objects later in this chapter, but for now you can think of a class as a template from which objects are made. When an object is created, we say that the objects are instantiated . Thus, if we create three employees, we are actually creating three totally distinct instances of an Employee class. Each object contains its own copy of the attributes and methods. For example, consider Figure 1.9. An employee object called John (John is its identity) has its own copy of all the attributes and methods defined in the Employee class. An employee object called Mary has its own copy of attributes and methods. They both have a separate copy of the DateOfBirth attribute and the getDateOfBirth method.

Figure 1.9. Program spaces.

graphics/01fig09.gif

An Implementation Issue

Be aware that there is not necessarily a physical copy of each method for each object. Rather, each object points to the same physical code. However, this is an implementation issue left up to the compiler/operating platform. From a conceptual level, you can think of objects as being wholly independent and having their own attributes and methods.


 <  Day Day Up  >  


Object-Oriented Thought Process
Object-Oriented Thought Process, The (3rd Edition)
ISBN: 0672330164
EAN: 2147483647
Year: 2003
Pages: 164
Authors: Matt Weisfeld

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net