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

Advantages offered by soa, Advantages offered by SOA Splits down the s...

Advantages offered by SOA Splits down the silos of data, functionalities, and applications into enterprise services. Adapts an application to changing business requirements

Brute force search, how to implement brute foce in a program?

how to implement brute foce in a program?

State about the performance of java, State about the performance of java ...

State about the performance of java First versions of java were pretty slow. Java performance has gotten a lot better with aggressive just-in-time-compiler (JIT) techniques

Write a short note on image preloading process, Write a short note on image...

Write a short note on image preloading process? The primary use of image preloading procedure is to download the image within the cache before it is actually required to displa

Write a java console application, You are to write a Java console applicati...

You are to write a Java console application, following the Software Life Cycle model, which will read personnel records from a text file and store them in a data structure.  One li

Uses of fileinputstream and fileoutputstream, Java Programming 1. Write...

Java Programming 1. Write a program in Java to find the highest of any five numbers. How do you compile and execute this Java program? 2. Write a program to explain the Exce

Types of dependency injection spring supports, What are the types of Depend...

What are the types of Dependency Injection Spring supports? Ans) there are two types of Dependency Injection Spring a)  Setter Injection: b)  Constructor Injection:

What is the use of forwardaction, Normal 0 false false fals...

Normal 0 false false false EN-IN X-NONE X-NONE MicrosoftInternetExplorer4

What are the services in rmi, An RMI "service" could well be any Java metho...

An RMI "service" could well be any Java method that can be invoked remotely. The other service is the JRMP RMI naming service which is a lookup service.

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