Guide to Java by James T. Streib & Takako Soma

Guide to Java by James T. Streib & Takako Soma

Author:James T. Streib & Takako Soma
Language: eng
Format: epub
Publisher: Springer London, London


Using this new class, a programmer could create three different instances of the PointD class as follows:

PointD p1, p2, p3;

p1 = new PointD();

p2 = new PointD(1.0,1.0);

p3 = new PointD(p2);

Notice that the objects are being created using three different constructors. The only difference is the number of arguments. Further, since the first constructor ensures that coordinates referenced by p1 will be initialized to 0.0, the second constructor initializes the variables referenced by p2 via the arguments, and the third constructor makes a copy of the previous object which will be referenced by p3, the set methods do not need to be called. However, if the values in the points need to be changed later, the set methods are still there if necessary.

If a constructor is not included in a class by the programmer, the system will generate a default constructor. Should the programmer include a constructor without any parameters, then this constructor overrides the default constructor generated by the system. Although a bit confusing, this constructor provided by the programmer is also sometimes called a default constructor since it overrides the system default constructor. However, if one writes the two new constructors above, and a default constructor is not included by the programmer, then the system will not generate a default constructor. In such a case, were one to code a p1=new PointD(); statement, a syntax error would occur. The result is if one wants to override the system default constructor, it is a good idea to override it with a programmer-defined default constructor to avoid a possible syntax error. Even if overloading is not being used in the class, it is generally best for a programmer to include a default constructor and not rely on the system default constructor.

Just as constructors can be overloaded, so can methods. As with constructors, the name of the method can be the same, but the number of parameters, the types of the parameters, or the order of the different types of parameters must be different. For example, take the distance method from Sect. 5.1 which requires one parameter as shown again below:

public double distance(PointD p) {

double dist;

dist=Math.sqrt(Math.pow(x-p.getX(),2)

+ Math.pow(y-p.getY(),2));

return dist;

}

What if another method was needed to determine the distance of a point from the origin? Certainly one could invoke the method above by having one of the two points as the origin using the new constructors introduced in this section as follows:

PointD p1, p2;

p1=new PointD();

p2=new PointD(3.0,4.0);

dist = p2.distance(p1);

In this example, the default constructor initializes the coordinates of p1 to 0.0, and the second constructor initializes the coordinates of p2 to 3.0 and 4.0. But the assumption could be that the distance will be calculated from the origin, and it would be convenient not to need it as a parameter in the distance method. Such a method would look as follows:

public double distance() {

double dist;

dist=Math.sqrt(Math.pow(x,2)+ Math.pow(y,2));

return dist;

}

Instead of invoking the previous method with the dist = p2.distance(p1); statement, it could be invoked using the new method as follows:

dist = p2.distance();

Again, the name of the method is the same, but the number of parameters is different.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.