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

Bilbo board , Design and simulate the bilbo board which should include thre...

Design and simulate the bilbo board which should include three configurable bilbos and some application logic to verify tester operation. the system will be designed using proteus

Write program to passing arguments to methods, Write program to Passing Arg...

Write program to Passing Arguments to Methods ? It's generally considered bad form to access fields directly. Instead it is considered outstanding object oriented practice to

Develop e-commerce website, Develop E-Commerce Website I need an E-Comme...

Develop E-Commerce Website I need an E-Commerce website for my furniture shop which requires to an online store to buy products online. So, Skills required: HTML, Java, Gr

Java assignment, Java Project Introduction: In this project, you will ex...

Java Project Introduction: In this project, you will explore a few sorting algorithms. You will also test their efficiency by both timing how long a given sorting operation take

Implement a driver program to test out all constructors, Implement a class ...

Implement a class  Book  (Book.java). A book will have three instance variables: a  title  (a String, for example: "Big Java" or "The Hitchhiker's Guide to the Galaxy"), an  author

Program that receives two integer inputs from a user, Question 4 Write ...

Question 4 Write a program that receives two integer inputs from a user, an ending value, lastvalue and column, column. The program will display all numbers from 1 to lastvalue

Explain about the dynamic java, Dynamic JAVA Class and type informatio...

Dynamic JAVA Class and type information is kept around at runtime. This allows runtime loading and inspection of code in a very flexible way.

Data types in javascript, A value, the data assigned to a variable, may con...

A value, the data assigned to a variable, may contain any sort of data. Though, JavaScript considers data to fall into many possible types. Based on the type of data, certain opera

Difference between boolean & operator and && operator, How can we define th...

How can we define the difference between the Boolean & operator and the && operator ?

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