• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Another question from marcus green mock exam

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The above code compiles & runs fine.Shouldn't it give compile time error?Coz run method of Thread class is not overriden in child class?
 
Vivek Nidhi
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope if a class extends another class, its not mandatory to implement or override the methods in the super class. Only in case of implementing interface, we need to implement the interface methods or declare the sub class as abstract
Vivek Nidhi

[ October 20, 2003: Message edited by: Vivek Nidhi ]
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to override run() to compile unless you implement Runnable interface in a concrete class. This code seems to be fine, no new thread is created because the start() method is overridden and called just like any other regular method. Hope this helps.
 
Thomas De Vos
stable boy
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Holt class does not need to implement the run() method of the interface Runnable.
The class Thread implements the defintion of the run() method. The run() method will call the start() method of the Thread class, unless the sub-class implements the start() method. This is the case in the Holt class and this is also the place where you will put you Thread logic.
This is one way of starting a Thread. The other way is implementing the Runnable interface (actually the same as what the Thread class does). That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started.
PS: I don't like sub-classing the Thread class, I prefer to work with Interfaces.
Thomas De Vos
http://www.javacertificate.com
http://www.j2eecertificate.com
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic