Explain what is constructors, JAVA Programming

Assignment Help:

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. 


Related Discussions:- Explain what is constructors

Describe tostring() methods, Describe toString() Methods ? Print method...

Describe toString() Methods ? Print methods are general in some languages, but most Java programs operate differently. You can use System.out.println() to print any object. The

What is bean factory, A BeanFactory is like a factory class that having a c...

A BeanFactory is like a factory class that having a collection of beans. The BeanFactory holds Bean Definitions of multiple beans within itself and then instantiates the bean whene

Collection, how would I become expert in collection framework

how would I become expert in collection framework

List the java class library, list The Java Class Library? Java holds an...

list The Java Class Library? Java holds an extensive library of pre-written classes you can use in your programs. These classes are separated into groups known as packages. Th

Design and implement online food delivery system, You are required to  desi...

You are required to  design & implement  online  food delivery  system  using Java RMI technology.  This involves writing both the server and the client program(s). Client programs

JAVA Fundamentals, 15. Energy Drink Consumption A soft drink company recent...

15. Energy Drink Consumption A soft drink company recently surveyed 12,467 of its customers and found that approximately 14 percent of those surveyed purchase one or more energy dr

What is inheritance in java explain with example, What is Inheritance in ja...

What is Inheritance in java Explain with example? Code reusability is claimed to be a key advantage of object-oriented languages over non-object-oriented languages. Inheritance

How many jsp scripting elements , How many JSP scripting elements and what ...

How many JSP scripting elements and what are they? Ans) Three scripting language elements are there: a)      declarations, b)      scriptlets, c)       expressions.

Prepare a java program to read .gpx files, Prepare a Java program to read ....

Prepare a Java program to read .GPX files Project Description: Prepare a Java program (GPX_Tracks_to_Text.Java) for Windows to: - Read every .GPX file in a provide drive a

Simulated annealing, implement simulated annealing for cable company proble...

implement simulated annealing for cable company problem

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd