Article

Object Oriented Concepts in Java – Part 2

Page: 1 2 3 4 5 6 7 8 9 10 Next

Encapsulation with Accessors

Previously, we modified the pickNut method so that it would not accept too high a number, which would cause our CoconutTree to think it contained a negative number of coconuts. But there is a much simpler way to produce this unrealistic situation:

CoconutTree ct = new CoconutTree();    
ct.numNuts = -10;

How can we protect Object properties from being assigned values like this that don't make sense? The solution is to make the properties themselves private, and only permit access to them using methods. Here is an updated version of our CoconutTree class that makes use of this technique:

1  package com.sitepoint;    
2    
3  public class CoconutTree extends Tree {    
4    private int numNuts = 0;      
5    
6    public void growNut() {    
7      numNuts = numNuts + 1;    
8    }    
9    
10   public boolean pickNut(int numToPick) {    
11     if (numToPick < 0) return false;    
12     if (numToPick > numNuts) return false;    
13     numNuts = numNuts – numToPick;    
14     return true;    
15   }    
16    
17   public int getNumNuts() {    
18     return numNuts;    
19   }    
20    
21   public boolean setNumNuts(int newNumNuts) {    
22     if (newNumNuts < 0) return false;    
23     numNuts = newNumNuts;    
24     return true;    
25   }    
26 }

As you can see on line 4, the numNuts property is now private, meaning that only code within this class is allowed to access it. The growNut and pickNut properties remain unchanged; they can continue to update the numNuts property directly (the constraints in pickNut ensure that the value of numNuts remains legal). Since we still want code to be able to determine the number of nuts in a tree, we have added a public getNumNuts method that simply returns the value of the numNuts property. As for setting the number of nuts, we have added a setNumNuts method that takes an integer value as a parameter. That value is checked to ensure that it is positive or zero (since we can't have a negative number of nuts) and then sets the numNuts property to this new value.

These two new methods, getNumNuts and setNumNuts, are known as accessor methods; that is, they are methods used for accessing a property. Accessors are very typical of a well-designed object. Even in cases where any value is acceptable, you should make your objects' properties private and provide accessor methods to access them. Doing this allows your programs to exhibit an important feature of object oriented programming called encapsulation.

Encapsulation means that the internal representation of an object is separated from the interface it presents to other objects in your program. In other words, a programmer that uses your class only needs to know what the public methods do, not how they work. The advantage is that you can change how your class works to improve performance or add new features without breaking any code that relies on the methods provided by your original class.

For example, if you decided you wanted to represent each coconut as an individual object of class Coconut instead of using a single integer variable to keep count, you could make the necessary changes and still have the same four methods as the implementation above. Old code that was written with the original interface in mind would continue to work as before, while new code could take advantage of the new features (which would of course be provided by new methods).

As an exercise, rewrite the Tree class so that it correctly encapsulates its height property.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links