Saturday, September 6, 2025
HomeLanguagesJavaOverriding of Thread class start() method

Overriding of Thread class start() method

Whenever we override start() method then our start() method will be executed just like a normal method call and new thread wont be created. We can override start/run method of Thread class because it is not final. But it is not recommended to override start() method, otherwise it ruins multi-threading concept.




// Java program illustrate the concept of
// overriding of start() method
class Bishal extends Thread {
public void start()
    {
        System.out.println("Start Method");
    }
public void run()
    {
        System.out.println("Run Method");
    }
} class Geeks {
public static void main(String[] args)
    {
        Bishal thread = new Bishal();
        thread.start();
        System.out.println("Main Method");
    }
}


Output:

Start Method
Main Method

NOTE : In the above program, when we are calling start() method by an object of Bishal class, then any thread won’t be created and all the functions are done by main thread only.

Runtime stack provided by JVM for the above program:

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.
Another Example:




class GThread extends Thread
{
    public void run()
    {
        System.out.println("GThread: run()");
    }
   
    public void start()
    {
        System.out.println("GThread: start()");
    }
}
   
class GRunnable implements Runnable
{
    public void run()
    {
        System.out.println("GRunnable: run()");
    }
   
    public void start()
    {
        System.out.println("GRunnable: start()");
    }
}
   
public class MyTest 
{
    public static void main(String args[])
    {
        GThread gt  =  new GThread();
        GRunnable gr = new GRunnable();
        Thread thread  =  new Thread(gr);
        gt.start();
        thread.start();
    }
}


Output:

GThread: start()
GRunnable: run()

When GThread.start() is called, output is : “GThread:start()”. When we call, thread.start(), the new thread will call the run() method of runnable, and we will get the “GRunnable:run()” message.

This article is contributed by Bishal Kumar Dubey. If you like Lazyroar and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the Lazyroar main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS