Java Reference
In-Depth Information
return ( int )(computeStrength() other . computeStrength() ) ;
However,thiswouldhaveonlyworkedifthe computeStrength method returned an int
instead of a double . For example, if the two strengths are 3.1 and 3.0, then subtracting
them and converting the result to an integer will give us the number 0. However, this will
indicate that the strengths are equal, which is not the case.
Second, note that calling the computeStrength method on an object of type
FictionalCharacter does not result in a compilation error. The reason is that, although
the method is abstract, so is the FictionalCharacter class. This means that the method
will actually be called on an object that belongs to a subclass of the FictionalCharacter
class where the computeStrength method will be implemented. Java uses dynamic binding
to decide during program execution the method that needs to be called. For example, if we
have an object that refers to a Superhero , then the computeStrength method from the
Superhero class will be executed.
Note that the classes Superhero and Villain do not need to be modified. We will
slightly modify the FaceOff class. In the new implementation, only the fictional character
that is the strongest will be printed. Here is the new implementation.
import java . util . ;
public class FaceOff {
public static void main(String [] args) {
ArrayList < FictionalCharacter > characters = new ArrayList <> () ;
populateArray( characters ) ;
Collections . sort(characters);
System.out. println(characters .get(characters . size () 1)) ;
} ...
The Collections class is a utility class that contains only static methods. The sort
method of the class can take as input an ArrayList of objects and sort them. The last
line simply prints the last element of the sorted ArrayList . Note that something very
interesting is happening here. The reason that the sort method can sort the elements
is because it knows that they are comparable. In other words, a compilation error will
occur if the FictionalCharacter class did not implement the interface Comparable .By
implementing this interface, the FictionalCharacter class announces to the rest of the
world that its objects are Comparable .Asaresult,the sort method calls the compareTo
method on objects of type FictionalCharacter multiple times in order to sort the objects.
One can think of a call to the compareTo method as a fight between two fictional characters.
Random numbers are used to calculate their current strength and the one with the highest
strength wins. There is also a method sort that belongs to the utility Arrays class. It can
be used to sort an array of objects that implement the interface Comparable .
8.8 Access Privileges
There is a slight problem with our previous solution. Although the program works, the
design is not perfect. Remember that we introduced the compareTo method in order to hide
from the rest of the world the value of a fictional character's strength. After all, a random
number is used to determine this strength and maybe we do not want to reveal this secret
Search WWH ::

Custom Search