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

What is constructor chaining, What is constructor chaining and how is it ac...

What is constructor chaining and how is it achieved in Java? A child object constructor always first requires to construct its parent (which in turn calls its parent constructo

Type of messaging is given by jms, Point-to-Point : gives a traditional qu...

Point-to-Point : gives a traditional queue based mechanism where the client application forwards a message through a queue to typically one receiving client that accepts messages

How can we include images in a web page using html and java, How can we inc...

How can we include images in a web page using HTML and Java script?2011 Images in HTML It is quite straight forward to include gif and jpg images in an HTML webpage using the

Interfaces and generics, In this assignment, you are provided with an inter...

In this assignment, you are provided with an interface that contains a generic type. You are asked to create two classes that implement this interface. A. The Sequenced Interfac

Use the constructor, Can we use the constructor, instead of init(), to init...

Can we use the constructor, instead of init(), to initialize servlet? Ans) Yes. Of course you can use the constructor instead of init(). There's nothing to stop you. But you sho

Describe the ? operator in java langauge, Describe The ? operator in Java l...

Describe The ? operator in Java langauge? The conditional operator just works for assigning a value to a variable, using a value within a method invocation, or in a few other w

Railway reservation, what java stream is used for designing railway reserva...

what java stream is used for designing railway reservation form?

What is synchronous messaging, Synchronous messaging involves a client that...

Synchronous messaging involves a client that waits for the server to respond to a message. So if one end is down the whole communication will fail.

What is a proxy, A proxy is an object that is formed after applying advice ...

A proxy is an object that is formed after applying advice to a target object. When you think of client objects the target object and the proxy object are the similar.

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