run() is a method of the Thread class and the Runnable interface. It makes up the entire body of a thread. It is the only method in which a thread’s behavior can be implemented. The return type of the run() method must be void. In Java, it is quite easy to overload the run() method. However, the overloaded run() method will be ignored by the Thread class unless it is called by the programmer. The Thread class expects a run() method without any argument and will execute the overloaded method when the programmer calls the method by a separate call. For example:
class MyThreadDemo extends Thread
{
public void run()
{
System.out.println(“job running”);
}
public void run(String str)
{
System.out.println(“The running string is:” +str);
}
}
Responses to “What will happen if a user tries to overload the run() method?”