<< Chapter < Page | Chapter >> Page > |
Lazy programming practice
It would be better programming practice to provide two explicit import directives, one for the Random class and the other for the Date class. However, if you are lazy like I apparently was when I wrote thisprogram, you can use the wildcard character (*) to import all of the classes in a package.
An abstract method
I'm going to begin by skipping down to the second line from the bottom in Listing 1 and explain the declaration of the abstract method named getData .
Purpose of an abstract method
The purpose of an abstract method declaration is to establish the signature of a method that must be overridden in every (non-abstract) subclass of the class in which the abstract method is declared.
An incomplete method
As you can see the abstract method has no body. Therefore, it is incomplete, has no behavior, and cannot be executed.
An abstract method must be overridden in a subclass in order to be useful.
Override in different ways
The same abstract method can be overridden in different ways in different subclasses. In other words, the behavior of the overridden version can betailored to (appropriate for) the class in which it is overridden.
A guarantee
The existence of an abstract method in a superclass guarantees that every (non-abstract) subclass of that superclass will have a concrete (executable) version of a method having that same signature.
An abstract class
The class named Prob04 is declared abstract in Listing 1 .
Any class can be declared abstract. The consequence of declaring a class abstract is that it is not possible to instantiate an object of the class.
Must be declared abstract...
More importantly, a class must be declared abstract if it contains one or more abstract method declarations. The idea here is that it must not bepossible to instantiate objects containing incomplete (non-executable) methods.
The main method
As you have seen in previous modules, the driver class for every Java application must contain a method named main with a signature matching that shown in Listing 1 .
A pseudo-random number generator
I will leave it as an exercise for the student to go to the javadocs and read up on the class named Random , along with the class named Date and the method named getTime .
Why pseudo-random?
I refer to this as a pseudo-random number generator because the sequence will probably repeat after an extremely large number of values has been generated.
An object of the class Random
Briefly, however, the first statement in the main method in Listing 1 instantiates an object that will return a pseudo-random number each time certain methods are called on the object.
Seeding the generator
The value passed as a parameter to the Random constructor represents the current time and guarantees that the series of pseudo-random values returnedby the methods will be different each time the program is run. This is commonly known as seeding the generator.
Get and save a pseudo random value
The next statement in Listing 1 1 calls the nextInt method on the generator object to get and save the next value of type int in the pseudo-random sequence.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?