Java Reference
In-Depth Information
The set layouts is expected to contain just one item because the number of compon-
ents for both the navigator and the widget being added is 1. However, the getLayoutS-
ize() method returns 2.
The reason for this discrepancy is that the hashCode() method of Widget is used only
once when the widget is added to the set. When the navigator is added, the hashCode()
method provided by the Navigator class is used. Consequently, the set contains two dif-
ferent object instances.
Compliant Solution ( final class)
Thiscompliantsolutiondeclaresthe Widget classfinalsothatitsmethodscannotbeover-
ridden:
public final class Widget {
// ...
}
Noncompliant Code Example ( run() )
Inthisnoncompliantcodeexample,class Worker anditssubclass SubWorker eachcontain
a startThread() method intended to start a thread.
Click here to view code image
public class Worker implements Runnable {
Worker() { }
public void startThread(String name) {
new Thread(this, name).start();
}
@Override
public void run() {
System.out.println("Parent");
}
}
public class SubWorker extends Worker {
@Override
public void startThread(String name) {
super.startThread(name);
new Thread(this, name).start();
}
@Override
Search WWH ::




Custom Search