22. Private methods
All of the methods we have used in our classes so far have been
public methods. It is quite often necessary to define
private methods in our classes though.
private methods are visible only to the methods of the class to which they belong, i.e. they cannot be called from outside the class by another object. By creating
private methods within a class, any of the other methods in the class can use these methods, but they are hidden from other classes. The advantage of having
private methods is that it promotes efficiency. You can write a block of code just once, and then call it elsewhere inside the class as often as necessary. This saves having to write the same code over and over every time you need the result it returns, in a method. At the same time, by making the method
private you can hide it from all other classes that don't need to know that it exists, because it is only required for execution of code inside, and not outside, the class.
Note that variables defined and only used inside a method (i.e. local variables) have scope that method only; it's not necessary to declare them to have any visibility themselves such as
THE Lotto CLASS
You've probably all bought a lottery ticket at least once in your lives, and some of you are likely to play most weeks. Consider the Lotto class illustrated below, and note its private method sort(int intArray) that sorts an array of integers in ascending order. This method is called by the public static method getTickets(int numTickets) in the statement
tickets[m] = sort(tickets[m]);
near the end of getTickets() below. This method has been separated from the getTickets() method because it is a generic sorting algorithm, i.e. it can be used to sort any array of integers into ascending order. Having built this method it can be easily copied into any other class that might need it if we keep it as a separate block of code. getTickets() is in the business of generating sets of 6 unique integers between 0 and 50, and sort() is in the business of sorting numbers. We keep them separate and simply call sort() whenever we need it.
Note that getTickets() is a static method of Lotto and can therefore be called without instantiating a Lotto object:
int theTickets = new int; theTickets = Lotto.getTickets(4);
Below are an images of the Lotto class in action.
The Lotto class is designed to return an array of arrays, but in the image above we see the sets of lottery numbers written to the console along with some other text. Why don't we just design Lotto to return the sets of numbers in this same way? Well, if we do that, the Lotto class becomes too rigid to be used for any purpose other than to produce and write a string (or several strings) to the console. By rather designing the class just to return an array we give users of the class the freedom to make their own decision about how they want to display the numbers, or even what exactly they want to do with the numbers if they don't want to display them for some reason. When designing your classes you should always strive to make them as adaptable as possible without compromising the security or integrity of the class. A class TestLotto was implemented to demonstrate the Lotto class in action, and TestLotto was responsible for the output you see above. The code for TestLotto appears below, but this code could have been written in any other way that would display the numbers.
Lotto is a helper class for TestLotto - it helps TestLotto to do its job, which is to gather and format for display a series of lottery numbers sets. TestLotto in other words is designed to produce a specific result; Lotto is designed to produce a generic result - one that can be bent to a wide variety of purposes by other classes. In particular, when designing a class for general use you can never make any assumptions about output from your class. This is because you cannot make any assumptions about how and where they will be used. Even assuming that the device on which the class will be used will at least have a screen, is dicey. Make the output of your class deliver a data type of some kind that can be configured for display by the implementing class.