Attributes

 <  Day Day Up  >  

Attributes represent the state of the object because they store the information about the object. For our example, the Cabbie class has attributes that store the name of the company, the name of the cabbie, and the cab assigned to the cabbie. For example, the first attribute stores the name of the company:

 
 private static String companyName = "Blue Cab Company"; 

Note here the two keywords private and static . The keyword private signifies that a method or variable can be accessed only within the declaring object.

Hiding as Much Data as Possible

All the attributes in this example are private. This is in keeping with the design principle of keeping the interface design as minimal as possible. The only way to access these attributes is through the method interfaces provided (which we will explore later in this chapter).


The static keyword signifies that there will be only one copy of this attribute for all the objects instantiated by this class. Basically, this is a class attribute. (See Chapter 3, "Advanced Object-Oriented Concepts," for more discussion on class attributes.) Thus, even if 500 objects are instantiated from the Cabbie class, there will be only one copy in memory of the companyName attribute (see Figure 4.2).

Figure 4.2. Object memory allocation.

graphics/04fig02.gif

The second attribute, name , is a string that holds the name of the cabbie:

 
 private String name; 

This attribute is also private so that other objects cannot access it directly. They must use the interface methods .

The myCab attribute is a reference to another object. The class, called Cab , holds information about the cab, such as its serial number and maintenance records:

 
 private Cab myCab; 

Passing a Reference

It is likely that the Cab object was created by another object. Thus, the object reference would be passed to the Cabbie object. However, for the sake of this example, the Cab is created within the Cabbie object. Likewise, for the purposes of this example, we are not really interested in the internals of the Cab object.


Note that at this point, only a reference to a Cab object is created; there is no memory allocated by this definition.

 <  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