Interview Questions

What will happen if we override start method?

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

38. What will happen if we override start method?

This question will again test your basic core java knowledge how overriding works at runtime, what what will be called at runtime and how start and run methods work internally in Thread Api.

When we call start() method on thread, it internally calls run() method with newly created thread. So, if we override start() method, run() method will not be called until we write code for calling run() method.

class MyThread extends Thread {
@Override
public void run() {
System.out.println("in run() method");
}


@Override
public void start(){
System.out.println("In start() method");
}
}

public class OverrideStartMethod {
public static void main(String[] args) {
System.out.println("main has started.");

MyThread thread1=new MyThread();
thread1.start();

System.out.println("main has ended.");
}
}

/*OUTPUT
main has started.
In start() method
main has ended.
*/


If we note output. we have overridden start method and didn’t called run() method from it, so, run() method wasn’t call.

(Continued on next question...)

Other Interview Questions