<< Chapter < Page Chapter >> Page >

Override the toString method

The ultimate superclass of every class is the predefined system class named Object . The Object class defines eleven methods with default behavior, including the method named toString .

Listing 4 overrides the inherited toString method, overriding the default behavior of the method insofar as objects of the class named Prob04MyClass are concerned.

Listing 4 . Override the toString method.
public String toString(){//overridden method return "Baldwin";}//end overloaded toString() }//end class Prob04MyClass

Default behavior of the toString method

If the toString method had not been overridden in the Prob04MyClass class, calling the toString method on an object of the class would return a string similar to that shown in Figure 2 .

Figure 2 . Default behavior of the toString method .
Prob04MyClass@42e816

Figure 2 shows the default behavior of the toString method as defined in the Object class. For this program, only the six hexadecimal digits at the end would change from one run to the next.

More on the default behavior of the toString method

Furthermore, if the toString method had not been overridden in the Prob04MyClass class, the output produced by the program on the command line screen would be similar to that shown in Figure 3 instead of that shown in Figure 1 .

Figure 3 . More on the default behavior of the toString method.
Prob04 DickProb04MyClass@42e816 -34-34

Compare to see the difference

If you compare Figure 3 with Figure 1 , you will see that the difference results from the fact that the overridden version of the toString method in Listing 4 returns "Baldwin" as a string rather than returning the default string shown in Figure 2 .

The end of the class named Prob04MyClass

Listing 4 signals the end of the class definition for the class named Prob04MyClass . Therefore, it is time to return to the explanation of the driver class shown in Listing 1 .

Display information about the object

When the Prob04MyClass constructor returns, Listing 1 calls the println method passing a reference to the new object as a parameter.

Many overloaded (not overridden) versions of println

There are many overloaded versions of the println method, each of which requires a different type of incoming parameter or parameters.

For example, different overloaded versions of the method know how to receive incoming parameters of each of the different primitive types, convert them tocharacters, and display the characters on the screen.

An incoming parameter of type Object

There is also an overloaded version of the println method that requires an incoming parameter of type Object . That is the version of the method that is executed when the reference to this object is passed tothe method in Listing 1 .

One object, several types

Recall that the reference to this object can be treated as its true type, or as the type of any superclass. Therefore, the reference can be treated asany of the following types:

  • Prob04MyClass
  • Prob04
  • Object

Will satisfy type requirement...

Because it can be treated as type Object , it will satisfy the type requirement for the overloaded version of the println method that requires an incoming parameter of type Object .

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask