<< Chapter < Page | Chapter >> Page > |
Among other things, this program illustrates:
Will explain in fragments
I will explain this program in fragments. A complete listing of the program is provided in Listing 13 near the end of the module.
Beginning of driver class for Prob05
The driver class for Prob05 begins in Listing 1 .
Listing 1 . Beginning of driver class for Prob05. |
---|
import java.util.*;
class Prob05{public static void main(String[] args){Random generator = new Random(new Date().getTime());
int randomData = (byte)generator.nextInt();Object[] var1 = new Object[2];
var1[0]= new Prob05MyClassA(randomData);
var1[1]= new Prob05MyClassB(randomData); |
Behavior of the code in Listing 1
Listing 1 does the following:
The same random value is passed to the constructor for both objects when they are instantiated.
Put the driver class on temporary hold
At this point, I am going to put the driver class named Prob05 on temporary hold and explain the class named Prob05MyClassA .
The interface named Prob05X
Having glanced ahead, I know that the class named Prob05MyClassA implements the interface named Prob05X so I will explain that interface first.
The interface named Prob05X is shown in its entirety in Listing 2 .
Listing 2 . The interface named Prob05X. |
---|
interface Prob05X{
public int getModifiedData();public int getData();
}//end interface |
An interface definition
An interface definition can contain only two kinds of members:
By now, you should have studied interfaces in my online tutorials. Therefore, this explanation will be very brief.
Method declarations
Listing 2 contains two method declarations.
A method declaration does not have a body. Its purpose is to establish the programming interface for that method in any class that implements the interface (return type, name, arguments, etc.) .
A method declaration provides no information about the behavior of the method.
A method declaration in an interface is implicitly abstract.
A concrete definition is required
Any class that implements an interface:
The class named Prob05MyClassA
The class named Prob05MyClassA , which implements the interface named Prob05X , must provide concrete versions of the methods named:
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?