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.

We will now look at how we can stop this public access to the data fields.  The protection of the data is called data encapsulation.

PUBLIC VS PRIVATE

 

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.

 

public class SillyPoint

{

   public double x;

   private double y;

}

 

public class SillyPointTester

{

   public static void main(String[] args)

   {

      SillyPoint sp = new SillyPoint();

      sp.x = 10.2;

      sp.y = 2.4;  //compiler error, y is private

 

      System.out.println(sp.x);

      System.out.println(sp.y);  //compiler error, y is private

   }

}

 


Similarly, a private method is only accessible from inside the class.  It is rare to use private methods but this is sometimes useful.  You will most likely not use these in the course.

 

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? 

Data field values can still be changed by using instance methods.  This allows the creator of the class to control how the data fields can be changed.  For example, the creator might opt to provide methods that only change certain data fields but not others.  Also, a method might only apply specific changes. 


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

 

  • Here is an example of the Point class with both set and get 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;

   }

}

 

  • We can now view the values of a Point as well as change the coordinate values of a point.

 

             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

 

  • It should be noted that set and get methods can be called anything.  The use of set/get and the data field name is simply a convention that is used to teach Object Oriented Programming.