3 Replies - 392 Views - Last Post: 11 May 2013 - 02:37 PM Rate Topic: -----

#1 Java.Programmer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 01-January 13

Question about creating a thread using extends

Posted 10 May 2013 - 10:03 PM

Hello all, I have a question about creating threads using extends (inheritance).

class MyThread extends Thread {
MyThread(){
super("My Thread");
start();
}
pubic void run()
{
System.out.println("child thread started");
System.out.println("Child thread terminated");
}
}



class demo {
public static void main(String args[]){
new MyThread();
System.out.println("Main thread started");
System.out.println("Main thread terminated");
}
}






My question is: what is happening in the first class that is defining the thread and how does it work? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Question about creating a thread using extends

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8032
  • View blog
  • Posts: 13,745
  • Joined: 19-March 11

Re: Question about creating a thread using extends

Posted 10 May 2013 - 10:50 PM

In that class, you're defining a constructor and a method. The constructor calls its superclass constructor with a String as an argument, and then it calls its start method.

The other method you've defined is called "run"; this is used to define the behavior that this thread exhibits when it is start()ed. In this case, it prints two messages.

There's a lot more about threads in this tutorial.
Was This Post Helpful? 0
  • +
  • -

#3 Java.Programmer  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 01-January 13

Re: Question about creating a thread using extends

Posted 11 May 2013 - 02:02 PM

View Postjon.kiparsky, on 10 May 2013 - 10:50 PM, said:

In that class, you're defining a constructor and a method. The constructor calls its superclass constructor with a String as an argument, and then it calls its start method.

The other method you've defined is called "run"; this is used to define the behavior that this thread exhibits when it is start()ed. In this case, it prints two messages.

There's a lot more about threads in this tutorial.


Quote

So to be more specific, what is the super followed by the string doing, and how is the start() method related to the super keyword?

Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Question about creating a thread using extends

Posted 11 May 2013 - 02:37 PM

Be more specific in the first place, and don't add responses to posts in quotes. One doesn't quote themselves unless highlighting a previous statement, which doesn't exist in this case.

super() calls the parent class' constructor with the appropriate signature. Since the parent is 'Thread', you can look up the Thread constructors in the API, and you'll find one with a similar signature. From there, you can figure out yourself what's going on.

If you need help finding these references, please ask, but first remember that Google is your friend.

This post has been edited by GregBrannon: 11 May 2013 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1