explain what is constructors it is frequent the


Explain what is constructors ?

It is frequent the case in which overloaded techniques are essentially the similar except that one supplies default values for a few of the arguments. In this case, your code will be easier to read and maintain (though perhaps marginally slower) if you put all your logic in the method which takes the most arguments, and simply invoke those techniques from all its overloaded variants which merely fill in appropriate default values.

This technique should also be used while one method needs to convert from one kind to another. For example one variant can convert a String to an int, then invoke the variant in which takes the int as an argument.

This straight-forward for regular methods, other than doesn't quite work for constructors because you can't simply write a method like this:

public Car(String licensePlate, double maxSpeed) {

Car(licensePlate, 0.0, maxSpeed);
}
Instead, to invoke another constructor in the same class from a constructor you use the keyword this like so:
public Car(String licensePlate, double maxSpeed) {

this(licensePlate, 0.0, maxSpeed);
}
Must this be the first line of the constructor?
For example,
public class Car {

private String licensePlate; // e.g. "New York A456 324"
private double speed; // kilometers per hour
private double maxSpeed; // kilometers per hour

// constructors
public Car(String licensePlate, double maxSpeed) {

this(licensePlate, 0.0, maxSpeed);

}

public Car(String licensePlate, double speed, double maxSpeed) {

this.licensePlate = licensePlate;
if (maxSpeed >= 0.0) {
this.maxSpeed = maxSpeed;
}
else {
maxSpeed = 0.0;
}

if (speed < 0.0) {
speed = 0.0;
}

if (speed <= maxSpeed) {
this.speed = speed;
}
else {
this.speed = maxSpeed;
}

}

// other methods...

}

This approach saves various lines of code. In also means that if you later required to change the constraints or other aspects of construction of cars, you only required to modify one method rather than two. This is not only simpler; it gives bugs fewer opportunities to be introduced either by inconsistent modification of multiple methods or through changing one method but not others. 

Request for Solution File

Ask an Expert for Answer!!
JAVA Programming: explain what is constructors it is frequent the
Reference No:- TGS0284612

Expected delivery within 24 Hours