Java TOPIC 40 – DATA ENCAPSULATION LESSON NOTE LEAD-IN We have
already seen that we are to use constructors to initialize all data
fields. You were told that one of the
reasons was to make sure the data fields were given “legal” values. The problem is that right now, we can
access a data field at anytime and change its value. This almost defeats the purpose of the
constructor. So far, we
have been placing “public” to the left of our data field declarations, our
constructor prototypes and our method prototypes. This means that all these components are
accessible from outside the class by simply using: objectName.dataFieldName or objectName.methodName(…) A private
data field on the other hand cannot be accessed from outside the class using
the above approach. You get a compiler
error if you try this. If one does
wish allow a private data field to be changed, then instance methods can be
used.
Note that
a private data field is used identically as a public data field from inside
the class. GET METHODS One
problem with private data fields is that we cannot see their value from
outside the class. To get their value,
we often use a get method. If we want
the value of a data field to be viewable (but not changeable), we provide a
get method for that private data field.
All it does is return the data field.
The naming convention which is most usually used is: getDataFieldName() For
example, assume a class has a private int data
field named length. We can allow it to
be viewed (not changed) by providing this simple method: public int getLength() { return length; } POINT CLASS EXAMPLE WITH GET METHODS
Here is a
Point class that has private data fields.
However, for the user to be able to see the values of these data
fields, get methods are provided. public class Point { private
double x; private
double y; public Point(double xcoord, double ycoord) { x = xcoord; y = ycoord; } public
double getX() { return
x; } public
double getY() { return
y; } } From a main method, we can now view
the values of the data fields by using the get methods. public class PointTester {
public static void main(String[] args) {
//Create Point p with random coordinates.
Point p = new Point(10*Math.random(),
10*Math.random);
//Output coordinates to screen.
System.out.println(“x = “
+ p.getX());
System.out.println(“y = “
+ p.getY()); } } SET METHODS
We know how we can view the values
of private data fields using get methods.
However, what if we need to change the value of a data field? The simplest method to change a data
field is what we call a set method. In
its simplest form, it gets a value for a data field and sets that data field
to that value. For example, lets
assume there is a class that contains a private data field named length. It can be changed if we provide the
following set method. public
void setLength(double l) { length = l; } Set methods can also have conditions
such as changing the data field if the new number is within a certain
range. For example, consider the above
example. The data field length will
only get the new value provided to the set method if that value is positive. public void setLength(double l) {
if (l >= 0)
{ length = l;
}
else
{ System.out.println(“Error
– length cannot be negative”);
} } POINT CLASS EXAMPLE
WITH GET & SET METHODS
public class Point { private
double x; private
double y; public Point(double xcoord, double ycoord) { x = xcoord; y = ycoord; } public
double getX() { return
x; } public double
getY() { return
y; } public
void setX(double newX) { x = newX; } public
void setY(double newY) { y = newY; } }
Point p = new
Point (0,0); p.setX(34.5); p.setY(14.9); System.out.println(“x = “ + p.getX()); System.out.println(“y = “ + p.getY()); COMMENT
|
||
|
||
|