• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runnable vs Thread

 
Shreesha Kramadhary
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does extended thread's run() is chosen over Runnable's run()



Ouput
Dog
 
Himai Minh
Ranch Hand
Posts: 1294
6
  • Mark post as helpful
  • send pies
  • 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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, because it is t.start(); not r.start() . clear more ?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch Shreesha Kramadhary
 
Kevin Florish
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • 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
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1294
6
  • Mark post as helpful
  • send pies
  • 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:
 
Bhoopendra Singh
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic