Java Reference
In-Depth Information
Tutorial: Overriding the toString Method
Let's prove that the toString method controls what is displayed by overriding the inherited Component toString method
in several of our classes.
1.
Display the Employee source code.
Click on Source, Override/Implement Methods... to display the “Override/Implement
Methods” window (Figure 5-5 .)
2.
Figure 5-5.
The “Override/Implement Methods” window displays all the methods Employee inherited. Because Employee
is a direct subclass of Object , only the methods inherited from Object are displayed. (Other classes, such as
EmployeeFrame, will show many more superclasses and inherited methods.)
3.
Click on the toString() check box as in Figure 5-5 , and then click the OK button.
RAD will insert the following toString method (referred to as a stub method).
public String toString() {
return super.toString();
}
4.
Change the return statement to the following so that the toString method returns the
EmpName.
return getEmpName();
 
Search WWH ::




Custom Search