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

Different messaging paradigms jms supports, What are the different messagin...

What are the different messaging paradigms JMS supports? Ans) Publish and Subscribe i.e. pub/suc and Point to Point i.e. p2p.

Java collection framework., #question.Write a program that computes the edi...

#question.Write a program that computes the edit distance (also called the Levenshtein distance, for its creator Vladimir Levenshtein) between two words. The edit distance between

Explain java.lang.string library in java, Explain java.lang.string library ...

Explain java.lang.string library in java? Strings are objects. Specifically they're instances of the class java.lang.String . This class has several methods which are useful f

Explanation of the inputstream with suitable diagrams, Problem 1) Descr...

Problem 1) Describe the concept of interfaces in Java with a suitable example for the same Explanation of the concept of interfaces with example 2) Describe the Inp

Program will compile but wont run , Im receiving an error message of Except...

Im receiving an error message of Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 at Homework.main(Homework.java:51)

I want to copy an iphone app with some changes, I want to copy an iphone ap...

I want to copy an iphone app with some changes Project Description: I want to copy an iphone app with some changes i need the best programmer to do this for me Skills r

Spring framework, Spring framework The Spring framework is the leading...

Spring framework The Spring framework is the leading full-stack J2EE /JAVA application framework. Not like other applications, Spring does not expose itself on the design of a

What is JMS queue, Staging areas that have messages those have been sent an...

Staging areas that have messages those have been sent and are waiting to be read. Note that, contrary to what the name queue suggests, messages don't have to be delivered in the or

Client for nokia symbian platform, Project Description: I need Team spea...

Project Description: I need Team speak 3 client for Nokia Symbian platform. Project for Nokia E72, but should work on all Java/Symbian mobiles. Feature list should include

N-th padovan string p(n), write a program that count the number of occurenc...

write a program that count the number of occurences of string in the n-th padovan string p(n)   program in java // aakash , suraj , prem sasi kumar kamaraj college progr

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