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

Develop a computer program and application, Develop a Computer program and ...

Develop a Computer program and application. Project Description: I want a project done. It is a computer desktop application program. It is not very hard. If you need the det

43560 square feet., calculate the size of the tract of land by the size of ...

calculate the size of the tract of land by the size of an acre to get the number of acres.

Test phases and cycles? , 1. Unit tests :There are two popular processe...

1. Unit tests :There are two popular processes to testing server-side classes: mock objects, which test classes by checking the server container, and in-container testing, which

How to use dispatchaction, To use the DispatchAction, follow these steps: ...

To use the DispatchAction, follow these steps: ? Make a class that extends DispatchAction (instead of Action) ? In a new class, add a method for each function you need to per

Retrieving data from Excel file , how to retrieve data from Excel file, and...

how to retrieve data from Excel file, and display that data in table on jsp page?

What is messaging, Messaging is a method of communication among software co...

Messaging is a method of communication among software components or applications. A messaging system is a peer-to-peer facility: A messaging client can send messages to, and receiv

What is the use of throws keyword, What is the use of throws keyword ? ...

What is the use of throws keyword ? Rather than explicitly catching an exception you can declare in which your method throws the exception. This passes the repsonsibility to h

Simulated annealing, implement simulated annealing for cable company proble...

implement simulated annealing for cable company problem

Intro to Programming, Overall Requirements Every phone number is broken up ...

Overall Requirements Every phone number is broken up into sections as shown below: Country Code Area Code Prefix Line Number 1 919 882 5000 Write a program to separate out a ph

Java, 1. Which of the following are not valid Java identifiers, and why? (...

1. Which of the following are not valid Java identifiers, and why? (a) wolVes (b) United(there is only one) (c) 87 (d) 5 3 (e) Real ale (f) isFound?by 2. A class Television

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