Monday, August 20, 2007

What is multiple constructors?

The arguments of a constructor specify the parameters for the initialization of an object. Multiple constructors provide the flexibility of initializing objects in the same class with different sets of arguments, as in the following example. public class Particle
{
double x, y, vx, vy, mass;
// examples of multiple constructors
public Particle()
{
this(0.0,0.0);
}
public Particle(double x, double y)
{
this(x,y,1.0);
}
public Particle(double x, double y, double m)
{
this(x,y,0.0,0.0,m);
}
public Particle(double x, double y, double vx, double vy)
{
this(x,y,vx,vy,1.0);
}
public Particle(double x, double y, double vx, double vy, double mass)
{
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.mass = mass;
}
}
The multiple constructors (all named Particle) are distinguished only by the number of arguments. The constructors can be defined in any order. Hence, the keyword this in the first constructor refers to the next in the sequence because the latter has two arguments. The first constructor has no arguments and creates a particle of unit mass at the origin; the next is defined with two arguments: the spatial coordinates of the particle. The second constructor in turn references the third constructor which uses the spatial coordinates and the mass. The third and fourth constructors each refer to the final constructors which uses all five arguments. (The order of the constructors is unimportant.) Once the Particle class with its multiple constructors is defined, any class can call the constructor Particle using the number of arguments appropriate to that application. The advantage of having multiple constructors is that applications that use a particular constructor are unaffected by later additions made to the class Particle, whether variables or methods. For example, adding acceleration as an argument does not affect applications that rely only on the definitions given above.
Using multiple constructors is called method overloading -- the method name is used to specify more than one method. The rule for overloading is that the argument lists for all of the different methods must be unique, including the number of arguments and/or the types of the arguments.
All classes have at least one implicit constructor method. If no constructor is defined explicitly, the compiler creates one with no arguments.
We next give an example of a Particle class with methods for computing the weight of a particle and its distance from the origin. We will omit the velocity components for now.public class Particle
{
double x, y, mass;
public Particle()
{
}
public Particle(double x, double y)
{
this(x,y,1);
}
public Particle(double x, double y, double mass)
{
this.x = x; this.y = y; this.mass = mass;
}
public double getWeight()
{
return 9.8*mass;
}
public double distanceFromOrigin()
{
return Math.sqrt(x*x + y*y);
}
}
The fundamental parts of a method are its name, arguments, return type, and the body. Methods can be created only as part of a class. By convention, variable and method names begin with a lower case letter.
The keyword return followed by an expression returns the value of the expression to the caller of the method. The type of the expression must be the same as specified by the method. (Note that a constructor does not return a value and hence no type is specified.) A method can return only the value of a single expression.
The method distanceFromOrigin in the class Particle computes the distance of a particle from the origin using the particle coordinates x and y. The square root function Math.sqrt in this calculation is part of the Math class, a library of mathematical functions which consists of a collection of static methods. We will postpone a discussion of such classes, but their use is clear from the context. A program consists of one or more class definitions, each of which should be in separate files. The name of the file should be the same as the name of the class, for example, MyApplication.java. One of these classes must define a method main.
The following program creates two objects of type Particle.public class MyApplication
{
public static void main(String[] args)
{
/* Need an instance of class Particle - a particle object;
assign it to the variable a which is of type Particle. */
Particle a = new Particle(1.0,1.0);
System.out.println("Distance of particle a from origin = " + a.distanceFromOrigin());
System.out.println("Mass of particle a = " + a.mass);
Particle b = new Particle();
b.x = 2.0;
b.y = 3.0;
b.mass = 3;
System.out.println("Distance of particle b from origin = " + b.distanceFromOrigin());
System.out.println("Mass of particle b = " + b.mass);
System.out.println("Weight of particle b = " + b.getWeight());
}
}
The first statement in main declares a to be a Particle and uses the new keyword to instantiate an object. These two operations could have been done separately in two lines as illustrated for Particle b. Note that Java already defines primitive data types such as int and double.
If a program has more than one class, one and only one of the classes must define main().
The statement System.out.println() allows us to print the argument to the standard output with a call to the System class. System.out is an object used for sending output to the screen and println is a method that this object invokes.

No comments: