What is constructors and explain with an example, JAVA Programming

Assignment Help:

What is Constructors? Explain with an example?

A constructor forms a new instance of the class. It initializes all the variables and does any work essential to prepare the class to be used. In the line

Car c = new Car();
Car() is the constructor. A constructor has the similar name as the class.
If no constructor exists Java gives a generic one that takes no arguments (a noargs constructor), other than it's better to write your own. You make a constructor through writing a technique that has the same name as the class. Therefore the Car constructor is called Car().

Constructors do not have return types. They do return an instance of their own class, other than this is implicit, not explicit.
The subsequent method is a constructor that initializes license plate to an empty string, speed to zero, and maximum speed to 120.0.

Car() {
this.licensePlate = "";
this.speed = 0.0;
this.maxSpeed = 120.0;
}
Better yet, you can form a constructor which accepts three arguments and use those to initialize the fields as below.
Car(String licensePlate, double speed, double maxSpeed) {

this.licensePlate = licensePlate;
this.speed = speed;
if (maxSpeed > 0) this.maxSpeed = maxSpeed;
else this.maxSpeed = 0.0;
if (speed > this.maxSpeed) this.speed = this.maxSpeed;
if (speed < 0) this.speed = 0.0;
else this.speed = speed;

}
Or perhaps you always needs the initial speed to be zero, but needs the maximum speed and license plate to be specified:
Car(String licensePlate, double maxSpeed) {

this.licensePlate = licensePlate;
this.speed = 0.0;
if (maxSpeed > 0) this.maxSpeed = maxSpeed;
else this.maxSpeed = 0.0;

}


Related Discussions:- What is constructors and explain with an example

Rebuilding a server environment, Project Description: We prepare and ope...

Project Description: We prepare and operate smartphone application.(Picture sharing and social networking) But there are problems in server side. It's very slow. So we

Describe exception classes, Java Programming 1. Discuss any five buzz w...

Java Programming 1. Discuss any five buzz words in Java. 2. Describe exception classes and also explain common exceptions in java. 3. Describe primitive and abstract data

What is jar archives, What is JAR archives? JAR archives are ZIP archiv...

What is JAR archives? JAR archives are ZIP archives along with a different extension. They contain a hierarchy of files and directories. In spirit a JAR file can take the place

Method over ridding method in java, Q. Write short on the method over riddi...

Q. Write short on the method over ridding method in java? Ans. Method over ridding and dynamic binding: We have seen that a method in a super class is inherited by its subclass

Explain difference between the boolean and operator, What is the difference...

What is the difference between the Boolean & operator and the && operator? If an expression including the Boolean & operator is evaluated, both operands are evaluated. Then the

What is the basic difference between threads and processes? , A process is ...

A process is an execution of a code but a thread is a single execution sequence within the process. A process may contain multiple threads. A thread is sometimes named a lightweigh

What are the advantages of jms, One of the principal benefits of JMS messag...

One of the principal benefits of JMS messaging is that it's asynchronous. Thus not all the pieces require to be up all the time for the application to function as a whole.

Explain how many ways businesses monitor their employees, Explain how many ...

Explain how many ways businesses monitor their employees? • Systems are available which monitor almost every key stroke that an employee forms on a computer. • Systems are avai

What is java bytecode, What is java Bytecode Java programs (Source code...

What is java Bytecode Java programs (Source code) are compiled into a form known as Java bytecodes. Java compiler reads Java language source (.java) files, translates source

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