Page 306 - Introduction to Programming with Java: A Problem Solving Approach
P. 306

                272 Chapter 7 Object-Oriented Programming—Additional Details Elegance
Note that the use of programmer-defined constructors is never an absolute requirement. Although it would defeat their purpose, you could initialize instance constants when you declared them. And you can always instantiate an object with empty parentheses and then call an initialization method to initialize instance variables as we did earlier. So why bother to use programmer-defined constructors? If you want distinctive instance constants, you must initialize them in a constructor—the compiler won’t let you do it in a method. Whenever you need to initialize an object’s instance variables, it’s more elegant to do it with the constructor that instantiates the object. The constructor intimately ties instance constant and instance variable initializa- tion with object creation. Constructors simplify things by avoiding a separate initialization step, and you don’t need a separate name for them because they just use the class name. Bravo, constructors!
7.9 Overloaded Constructors
Overloading a constructor is like overloading a method. Constructor overloading occurs when there are two or more constructors with the same name and different parameters. Overloaded constructors are very com- mon (more common than overloaded methods). That’s because you’ll often want to be able to create objects with different amounts of initialization. Sometimes you’ll want to pass in initial values to the constructor. At other times, you’ll want to refrain from passing in initial values to the constructor, and rely on assigning values later on. To enable both of those scenarios, you need overloaded constructors—one constructor with parameters and one constructor without parameters.
            Apago PDF Enhancer
An Example
Suppose you want to implement a Fraction class, which stores the numerator and denominator for a given fraction. The Fraction class also stores the fraction’s quotient, which is produced by dividing the numerator by the denominator. Normally, you want to instantiate the Fraction class by passing a nu- merator argument and a denominator argument to a two-parameter Fraction constructor. But for a whole number, you want to instantiate a Fraction class by passing just one argument (the whole number) to a Fraction constructor, rather than passing two arguments. For example, to instantiate a 3 whole number as a Fraction object, you want to pass in just a 3 to a Fraction constructor, rather than a 3 for the numerator and a 1 for the denominator. To handle two-argument Fraction instantiations as well as one- argument Fraction instantiations, you need overloaded constructors. One way to begin solving a problem is to write a driver that shows how you want the solution to be used. With that in mind, we present a driver in Figure 7.18 that illustrates how the proposed Fraction class and its overloaded constructors can be used. The driver’s code includes line numbers to facilitate later tracing.
Assume that within the Fraction class, numerator and denominator are int instance vari- ables and quotient is a double instance variable. The two-parameter constructor should look some- thing like this:
 }
public Fraction(int n, int d)
{
this.numerator = n;
this.denominator = d;
this.quotient = (double) this.numerator / this.denominator;





















































































   304   305   306   307   308