<< Chapter < Page | Chapter >> Page > |
Listing 2 . Definition of the class named B. |
---|
class B extends A{
public String toString(){return "toString in class B";
}//end overridden toString()}//end class B |
Overrides the toString method
Of particular interest, for purposes of this module, is the fact that the class named B does override the inherited t oString method.
(The class named B inherits the default version of the method, because its superclass named A , which extends Object , does not override the toString method.)
Purpose of the toString method
The purpose of the toString method is to return a reference to an object of the class String that represents an object instantiated from a class that overrides the method.
Here is part of what Sun has to say about the toString method:
"Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method."
Behavior of the overridden version
As you can see, I didn't follow Sun's advice very closely in this program. To begin with, I didn't override the toString method in the class named A .
Further, the behavior of my overridden version of the toString method in the class named B doesn't provide much in the way of a textual representation of an object instantiated from class B .
My overridden version simply returns a reference to a String object, containing text that indicates that the overridden version of the method defined in the class named B has been executed. (Of course, there wasn't much about an object instantiated from the class named B that could be represented in a textual way.)
Will be useful later
The reference to the String object returned by the overridden version of the toString method will prove useful later when we need to determine which version of the method is actually executed.
The class named C
Listing 3 shows the definition of a class named C , which extends the class named B , and overrides the method named toString again. (A non-final method can be overridden by every class that inherits it, resulting in potentially many different overridden versions of a method in a class hierarchy.)
Listing 3 . Definition of the class named C. |
---|
class C extends B{
public String toString(){return "toString in class C";
}//end overridden toString()}//end class B |
Behavior of overridden version
The behavior of this overridden version of the method is similar to, but different from the overridden version in the class B .
In this case, the method returns a reference to a String object that can be used to confirm that this overridden version of the method has been executed.
The driver class
Finally, Listing 4 shows the beginning of the driver class named Poly04 .
Listing 4 . Beginning of the class named Poly04. |
---|
public class Poly04{
public static void main(String[]args){
Object varA = new A();String v1 = varA.toString();
System.out.println(v1); |
A new object of the class A
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?