<< Chapter < Page | Chapter >> Page > |
The new behavior is to construct and return a string version of the value obtained by adding 5 to the value stored in data , which is the original random value.
Listing 6 . Overridden toString method. |
---|
public String toString(){
return "" + (data + 5);}//end toString()}//end class Prob05MyClassA |
The end of the class named Prob05MyClassA
Listing 6 also signals the end of the class definition for the class named Prob05MyClassA .
The class named Prob05MyClassB
Referring back to the code in the driver class in Listing 1 , we see that the driver also instantiates an object of the class named Prob05MyClassB , passing the same random value to the constructor for the class.
The reference to the object is stored in the second element of the array object of type Object referred to by the reference variable named var1 .
Beginning of the class named Prob05MyClassB
The beginning of the class named Prob05MyClassB is shown in Listing 7 .
Listing 7 . Beginning of the class named Prob05MyClassB. |
---|
class Prob05MyClassB implements Prob05X{
private int data;Prob05MyClassB(int inData){
System.out.println("Put your last name here");data = inData;
}//end constructor |
Implements Prob05X
The first thing we notice is that this class also implements the interface named Prob05X . This requires that the class provide concrete definitions of the two methods declared in that interface.
Save the incoming parameter value
The constructor for the Prob05MyClassB class, which is shown in Listing 7 , saves the incoming parameter value in a private instance variable named data .
Unrelated to the variable named data from before
It is important to note that this variable named data is completely unrelated to the private instance variable named data that is declared in Listing 3 , even though they are the same type and they have the same name.
They belong to two different objects. Objects do not share instance variables.
The two objects are related
However, even though the two objects instantiated in Listing 1 are instantiated from different classes, they are related in the sense that they have two ancestors in common. They both extend the class named Object by default and they both explicitly implement the interface named Prob05X . That means that they can both be treated as either type Object or type Prob05X .
Related through the interface by design
Because all classes are direct or indirect subclasses of the class named Object , all objects instantiated for any class are related at the Object level. However, the objects in this program are related through the Prob05X interface only because I designed the program that way.
The method named getModifiedData
The method named getModifiedData is shown in Listing 8 .
Listing 8 . The method named getModifiedData. |
---|
public int getModifiedData(){
return data + 1;}//end getModifiedData() |
Same behavior is not required
A comparison of Listing 8 with Listing 4 exposes a very important aspect of interface implementation.
If two different classes implement the same interface, they each must provide concrete definitions of all the method declared in the interface. When providingsuch concrete definitions, both classes must match the method signatures of the declared methods.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?