«^»
11.2. Deriving the class ClockStdout from java.lang.Thread

However, because Java has inheritance, we can derive a class from java.lang.Thread and provide a run method that does something useful.

In the code below, a class called ClockStdout is derived from java.lang.Thread, and ClockStdout's declaration overrides Thread's run method. The code of ClockStdout's run method is an infinite loop inside which we first get the current date and time, then output that to the standard output, and then wait for two seconds.

0794: import java.util.Date;                                   // ClockStdout.java
0795: public class ClockStdout extends Thread {
0796:    public void run() {
0797:       while ( true ) {
0798:          Date tDate = new Date(); 
0799:          System.out.println(tDate);
0800:          try { Thread.sleep(2000); }
0801:          catch ( InterruptedException tInterruptedException ) { }
0802:       }
0803:    }
0804: }