July 31, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Object Construction

  • July 1, 2005
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

Using Multiple Constructors

In many cases, an object can be constructed in more than one way. To accommodate this situation, you need to provide more than one constructor. Consider the Count class presented below.

public class Count {
   int count;
   public Count(){
      count = 0;
   }
}

On the one hand, you simply want to initialize the attribute count to count to zero. You can easily accomplish this by having a constructor initialize count to zero as follows:

public Count(){
   count = 0;
}

On the other hand, you might want to pass an initialization parameter that allows count to be set to various numbers:

public Count (int number){
   count = number;
}

This is called overloading a method (overloading pertains to all methods, not just constructors). Most O-O languages provide functionality for overloading a method.

Overloading Methods

Overloading allows a programmer to use the same method name over and over, as long as the signature of the method is different each time. You do this quite often with constructors. The signature consists of the method name and a parameter list (see Figure 1).

Thus, the following methods all have different signatures:

public void getCab();
// different parameter list
public void getCab (String cabbieName);
// different parameter list
public void getCab (int numberOfPassengers);

Figure 1: The components of a signature.

By using different signatures, you can construct objects in various ways depending on the constructor used.

In some languages, the signature may also include the return value.

For example, in Java, the following code would generate a compiler error even though the return values are different.

public class Count {
   public int method01(int x){
      ...
   }
   public void method01(int x){
      ...
   }
}

Using UML to Model Classes

Look at a database reader example to illustrate some of the points pertaining to a constructor. Consider that we have two ways we can construct a database reader:

  1. Pass the name of the database and position the cursor at the beginning of the database.
  2. Pass the name of the database and the position within the database where we want the cursor to position itself.

Figure 2 shows a class diagram for the DataBaseReader class. Note that the diagram lists two constructors for the class. Although the diagram shows the two constructors, without the parameter list there is no way to know which constructor is which. To distinguish the constructors, you can look at the corresponding code listed below.

Figure 2: The DataBaseReader class diagram.

Notice that in this class diagram the constructors do not have a return type. All other methods besides constructors must have return types.

Here is a code segment of the class that shows its constructors and the attributes that the constructors initialize (see Figure 3):

public class DataBaseReader {
   String DBName;
   int startPosition;
   // initialize just the name
   public DataBaseReader (String name){
   DBName = name;
};
   // initialize the name and the position
   public DataBaseReader (String name, int pos){
      DBName = name;
      startPosition = pos;
   };
   .. // rest of class
}

Figure 3: Creating a new object.





Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel