Section 5.13. Encapsulation

   

5.13 Encapsulation

Encapsulation is an important aspect of OOP. In general, it means combining the data and the methods in the same object. The purpose of this is to make sure that unrelated, unforeseen functions cannot access or manipulate data in arbitrary ways ”it means controlling not only what will happen, but exactly how it will happen. The primary means of ensuring that your programs are encapsulated is to employ an object's interface and its implementation separately.

An interface is a primary means of object communication. Every class defines specific interfaces for how they must be properly instantiated . The interface describes completely how messages sent to an object can invoke its behavior.

Note

Any methods included as part of the interface must be declared public .


The interface of the Employee object above consists of two things: how to instantiate an Employee object; and how to send a parameter to that object, have it perform its work, and send a value back.

In general, only methods are part of the interface, not attributes. That should be obvious upon consideration: The interface describes how to interact with an object, not what is being interacted with per se. Methods that are hidden are not part of the interface.

As we have seen in previous examples, the way that Java and OOP are defined replicates structures in the real world in order to achieve similar benefits of organization and efficiency. In order to understand the need for interfaces, it is important to consider the implementation of a class. When a class is implemented; that is, when it is sent an external message in order to invoke some behavior, the sender of the message should not rely upon the inner workings of the class, how it performs its calculations, or arrives at its results ”it just wants the results in the expected format. The benefit of this kind of design is that the underlying code can change, and it will not break the relationship. The concept of code that is written to expose an implementation that is independent from the manner in which messages are sent to and from its objects is called encapsulation .

Many things in the real world act this way ”their functionality is encapsulated so that interacting with them is easy. Consider a toaster and a laptop computer. You plug in your toaster and it produces a fluffy piece of golden toast ”it does what a toaster does with the electricity from the outlet. Same thing with the computer. You plug it in and it does what a computer does. There is little relation between a toaster and a computer, except they both require electricity to run. The power plant produces electricity for consumption by electrical devices; all you have to do is plug a device into the outlet. The outlet is the interface. You don't need to know anything about the power plant or how it works, and neither does your toaster. The power plant is the implementation ”it is the way that power gets produced. It doesn't matter if it is coal-generated power or nuclear -generated power. Electricity is electricity. So any appliance that conforms to the interface can use the result of the implementation. You just need to have a plug that fits the socket. An outlet designed for a two-prong plug will not accommodate a three-prong plug.

Let's look at an example of how this works in Java:

 private int num;  // public interface public int getNum(int num) { num = doubleIt(num); return num; //private implementation private int doubleIt(int num) { return num + num; } 

In this example, the implementation is hidden. The doubleIt() method, which returns the supplied number doubled , is private. It is not directly accessible to be sent a message from outside this class. Any code wanting to use this method must instead call the getNum() method ”it must use the public interface. This way we can easily change the implementation of this method without breaking any code we write to access it. For instance, the code currently doubles the supplied number by adding it to itself. So, 5 + 5 = 10. However, we could later decide we wanted a more sophisticated way of doing this, say, by using * 2 (as in 5 * 2 = 10); because we have used encapsulation to shield the implementation from the interface, none of our existing code that interacts with this class will break.


   
Top


Java for ColdFusion Developers
Java for ColdFusion Developers
ISBN: 0130461806
EAN: 2147483647
Year: 2005
Pages: 206
Authors: Eben Hewitt

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net