Discussion Questions: Analyzing Method Visibility

Using Private "Helper" Methods


Introduction

The AquaFish class currently has two private "helper" methods, initSize and initPos, which determine the initial size and an initial location and direction for a newly constructed fish. All other methods are public, and all data members are private.

Questions

  • The AquaFish class has many observer methods (such as id, atWall, atBottom, etc). Could any of them be private without affecting the rest of the program? If so, would it be a good idea to make them private?
    Does the fact that the methods are public make the class more flexible or make it easier to add new functionality to the program?

  • The AquaFish class currently has several public modifier methods such as moveForward and changeDir. Could either of them be private without affecting the rest of the program? If so, would it be a good idea to make them private? Consider the issues listed above.

  • In the previous lab, you added two new methods, ascend and descend, to the AquaFish class. Each method causes the fish to move up or down by one y-coordinate value. Should the new methods be public or private? Why?

  • In the previous lab, you also added a new method move to the AquaFish class, which encapsulates all of the moving behavior of a fish. Should this method be public or private? Why?




Copyright Alyce Faulstich Brady, 2001. Modified by Pamela Cutter, 2002.