• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread question

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Main extends Thread{
public static void main(String[] args) {
Main m = new Main();
m.start();
}
}

I think this should not compile, but it does in NetBeans 6. Main extends thread wich extends Runnable wich has the "public abstract void run()"-method.

Main doesn�t implement all abstract method, but still, it compiles. Why?
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

In the example shown class Main extends
Thread class thats ok,but Thread does not extend
Runnable bcoz Runnable is an interface
which has got abstract method run()!!!
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Per Radfe,

run() method is implemented in Thread class, which is an empty method.
if we override the run method in 'Main class' it will be called by the start(),
otherwise it will look for Thread class run() method.So this program compiles.

Check the below from Java API

"If this thread was constructed using a separate Runnable run object,
then that Runnable object's run method is called;
otherwise, this method does nothing and returns."


Reeta
 
Per Radfe
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Reeta Champs!

I looked in the Thread class but couldn�t find the run method and thought that it didn�t implement it.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I looked in the Thread class but couldn�t find the run method and thought that it didn�t implement it.



Well, it's there
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes what Reeta answered is right
You can check in the below link that
run() method is implemented in Thread class,
http://www.docjar.com/html/api/java/lang/Thread.java.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic