12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

100 chapter 43. You should see now how OOP permits us to create many types of batonswith only slight modifications of the code. You can switch between aBaton and a LeadBaton object with only a single change to main, amodification that would be significantly more difficult with proceduralprogramming.4.9.9 Encapsulation to Protect ClassesIn the previous section we created the classes for Ball, Path, and Baton objects.In all cases the Java source code for each class had the same basic structure: classvariables, constructors, and get methods. Yet classes do different things, and it iscommon to categorize the functions of classes as either of the following.Interface: How the outside world manipulates an object; all methods that areapplied to that object; orImplementation: The actual internal workings of an object; how the methodsmake their changes.As applied to our program, the interface for the Ball class includes a constructorBall and the getM, getR, and getI methods. The interface for the Path class includesa constructor Path and the getX and getY methods.Pure OOP strives to keep objects abstract and to manipulate them only throughmethods. This makes it easy to follow and to control where variables are changedand thereby makes modifying an existing program easier and less error-prone. Withthis purpose in mind, we separate methods into those that perform calculations andthose that cause the object to do things. In addition, to protect our objects from beingmisused by outsiders, we invoke the private (in contrast to public) key word whendeclaring class variables. This ensures that these variables may be accessed andchanged only from inside the class. Outside code may still manipulate our objects,but it will have to do so by calling the methods we have tested and know will notdamage them.Once we have constructed the methods and made the class variables private,we have objects that are protected by having their internal codes entirely hiddento outside users. As programmers, we may rewrite an object’s code as we wantand still have the same working object with a fixed interface for the rest of theworld. Furthermore, since the object’s interface is constant, even though we maychange the object, there is no need to modify any code that uses the object. Thisis a great advance in the ability to reuse code and to use other people’s codesproperly.This two-step process of creating and protecting abstract objects is known asencapsulation.An encapsulated object may be manipulated only in a general mannerthat keeps the irrelevant details of its internal workings safely hidden within. Justwhat constitutes an “irrelevant detail” is in the eye of the programmer. In general,you should place the private key word before every nonstatic class variable and−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 100

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!