• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Runnable vs Thread

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does extended thread's run() is chosen over Runnable's run()



Ouput
Dog
 
Bartender
Posts: 2447
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm, because it is t.start(); not r.start() . clear more ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch Shreesha Kramadhary
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2447
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matthew is correct. You overriding default behaviour of Thread's Run method. So this method is doing what you are asking to do.
 
reply
    Bookmark Topic Watch Topic
  • New Topic