Give an examples of access protection, JAVA Programming

Assignment Help:

Give an Examples of Access Protection ?

This is how the Car class would almost certainly be written in practice. Remember that all the fields are now declared private, and they are accessed just by public methods. This is the general pattern for all but the easiest classes.
public class Car {

private String licensePlate; // e.g. "New York A456 324"
private double speed; // kilometers per hour
private double maxSpeed; // kilometers per hour

public Car(String licensePlate, double maxSpeed) {

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

}

// getter (accessor) methods
public String getLicensePlate() {
return this.licensePlate;
}

public double getSpeed() {
return this.speed;
}

public double getMaxSpeed() {
return this.maxSpeed;
}

// setter method for the license plate property
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}

// accelerate to maximum speed
// put the pedal to the metal
public void floorIt() {
this.speed = this.maxSpeed;
}

public void accelerate(double deltaV) {

this.speed = this.speed + deltaV;
if (this.speed > this.maxSpeed) {
this.speed = this.maxSpeed;
}
if (this.speed < 0.0) {
this.speed = 0.0;
}
}
}
In Several cases there will also be private, protected and default access techniques as well. Collectively these are known as non-public methods.

In several cases, the fields may be protected or default access. Therefore public fields are rare. This allows programmers to change the implementation of a class although still maintaining the similar contract with the outside world.
Dynamic vs static linking.


Related Discussions:- Give an examples of access protection

Code java and javascript in liferay, We need a serious programmer who will ...

We need a serious programmer who will code Java and Javascript in Liferay - open to bidding Project Description: Big Data project Prototype in Liferay. Big Data + User onl

What are inner beans, When wiring beans, if a bean element is embedded to a...

When wiring beans, if a bean element is embedded to a property tag directly, then that bean is said to the Inner Bean. The disadvantage of this bean is that it cannot be reused any

Midlet game, do you have example of a simple midlet game ?

do you have example of a simple midlet game ?

Develop billing software, Develop Billing Software Project Description: ...

Develop Billing Software Project Description: Need software to print bills on A4 paper in the format provided. The details are shown below - 1. Serially generated Invoice

Need professional framework coder with coding experience, Need Professional...

Need Professional Framework Coder with Video Coding Experience? Project Description: Professional Framework Coder with Video Coding experience. You must know API, Java, C++,

Develop the back end of a calculator application, For this assignment, you ...

For this assignment, you will develop the back end of a calculator application. The GUI (graphical user interface) has been provided by your instructor. The application will requir

Describe the superclass in inheritance, Describe the Superclass in Inherita...

Describe the Superclass in Inheritance ? In this example you start through defining a more common MotorVehicle class. public class MotorVehicle { private String licensePlat

Explain the three benefits of access protection, Explain the Three Benefits...

Explain the Three Benefits of Access Protection ? Access protection has three major benefits: 1. It permits you to enforce constraints on an object's state. 2. It gives a si

What are separators in java, What are separators in java? Separators he...

What are separators in java? Separators help elaborates the structure of a program. The separators used within HelloWorld are parentheses, ( ) , braces, { } , the period, .

JAVA Cipher Program, Cryptography, the study of secret writing, has been ar...

Cryptography, the study of secret writing, has been around for a very long time, from simplistic techniques to sophisticated mathematical techniques. No matter what the form howeve

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