File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Runnable vs Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Runnable vs Thread" Watch "Runnable vs Thread" New topic
Author

Runnable vs Thread

Shreesha Kramadhary
Greenhorn

Joined: Apr 14, 2009
Posts: 1
Why does extended thread's run() is chosen over Runnable's run()



Ouput
Dog
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 723
Thread t = new Thread(r){....}
What is t refering to ? A Thread or an anonymous subclass of Thread?
Thread t is refering to A Thread, which has a run method that prints "Dog".
Just like
We have Animal class and its subclass Cat.
Animal has eat(). Cat overrides eat().
Animal a =new Cat();
a.eat();
What is the result? It calls the eat method of Animal, not Cat.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hmm, because it is t.start(); not r.start() . clear more ?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Welcome to CodeRanch Shreesha Kramadhary
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 175
Himai Minh wrote:
We have Animal class and its subclass Cat.
Animal has eat(). Cat overrides eat().
Animal a =new Cat();
a.eat();
What is the result? It calls the eat method of Animal, not Cat.


Actually it calls the eat method of Cat, because with overrides the actual object type of Cat is dynamically called at runtime, not the reference type of Animal


Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

By default, the run() method of a Thread will delegate that call to the run() method of the Runnable passed into it (if anything was).

But this case isn't using the default. You've overridden Thread.run() in that instance to do something else. So it's going to do something else.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 723
Kevin Florish wrote:
Actually it calls the eat method of Cat, because with overrides the actual object type of Cat is dynamically called at runtime, not the reference type of Animal


Thanks, Kevin.

By the way, let me do it again.
Thread t actually overrides the run method that prints "Dog", like Animal a = new Cat(); a.eat(); This eat() is the Cat's eat(), not its parent's eat.
Why r's run() is not called? If your Cat class has an attribute , such as int claw that Animal class does not have, Animal class knows nothing about that Cat's claw.


The same thing here:
Bhoopendra Singh
Greenhorn

Joined: Feb 21, 2012
Posts: 3
Matthew is correct. You overriding default behaviour of Thread's Run method. So this method is doing what you are asking to do.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runnable vs Thread
 
Similar Threads
Multiple Threads Accessing Multiple Methods Independently
Given: ...
Question onThread
When both Thread and Runnable have public void run
UI is hanging