Constructors

 <  Day Day Up  >  

This Cabbie class contains two constructors. We know they are constructors because they have the same name as the class: Cabbie . The first constructor is the default constructor:

 
 public Cabbie() {       name = null;       myCab = null;    } 

Technically, this is not a default constructor. The compiler will provide a default constructor if you do not specify a constructor for this class. By definition, the reason it is called a default constructor here is because it is a constructor with no arguments. If you provide a constructor with arguments, the system will not provide a default constructor. The rule is that the default constructor is only provided if you provide no constructors.

In this constructor, the attributes Name and myCab are set to null :

 
 name = null; myCab = null; 

The Nothingness of Null

In many programming languages, the value null represents a value of nothing. This might seem like an esoteric concept, but setting an attribute to nothing is a useful programming technique. Checking a variable for null can identify whether a value has been properly initialized . For example, you might want to declare an attribute that will later require user input. Thus, you can initialize the attribute to null before the user is actually given the opportunity to enter the data. By setting the attribute to null (which is a valid condition), you can check whether an attribute has been properly set.


As we know, it is always a good idea to initialize attributes in the constructors. In the same vein, it's a good programming practice to then test the value of an attribute to see whether it is null . This can save you a lot of headaches later if the attribute or object was not set properly. For example, if you use the myCab reference before a real object is assigned to it, you will most likely have a problem. If you set the myCab reference to null in the constructor, you can later check to see whether myCab is still null when you attempt to use it. An exception might be generated if you treat an uninitialized reference as if it were properly initialized.

The second constructor provides a way for the user of the class to initialize the Name and myCab attributes:

 
 public Cabbie(String iName, String serialNumber) {     name = iName;     myCab = new Cab(serialNumber); } 

In this case, the user would provide two strings in the parameter list of the constructor to properly initialize attributes. Notice that the myCab object is actually instantiated in this constructor:

 
 myCab = new Cab(serialNumber); 

At this point, the storage for a Cab object is allocated. Figure 4.3 illustrates how a new instance of a Cab object is referenced by the attribute myCab . Using two constructors in this example demonstrates a common use of method overloading. Notice that the constructors are all defined as public . This makes sense because in this case, the constructors are obvious members of the class interface.

Figure 4.3. The Cabbie object referencing an actual cab object.

graphics/04fig03.gif

 <  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