Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

override sleep method

 
Nada Mohammed
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
in the code below
class a extends Thread {
public static void sleep(long m , int n ) throws
InterruptedException
{System.out.println("this is my sleep method") ;}
}
class b extends a {
public static void main(String [] args) {
try{sleep(10,9) ;} catch(Exception e) {}
}
}
class a override sleep method therefore when i call it form class b then the result will be :
this is my sleep method
my problem is when i need to call sleep method i must put it in try /catch so is there is any way to override sleep method and call this doverriedden method without try/catch and without write thorws.... in fornt of class b .
in anther way i need class a to be the base class and has sleep method .This method call from any extends class in a simple way
sleep(..,...) ;
anther question why sleep method static ??
pleeeease i need support
thanks
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Nada Mohammed:
[...] is there is any way to override sleep method and call this doverriedden method without try/catch and without write thorws.... in fornt of class b .
Your code will actually compile if you just remove the "throws InterruptedException" clause. You can then call "A.sleep()" without having to deal with the exception. Of course, it won't actually sleep, but you can fix that by just sticking in <code>try { super.sleep(m, n) } catch(InterruptedException e) { }</code> in A.sleep().
anther question why sleep method static ??
Well, what would it mean if you could call sleep() on another Thread? Should that thread be suspended for the given time? - And what would happen to the InterruptedException that might possibly be thrown? It's a checked exception; if a thread could be made to sleep at arbitrary moments then an InterruptedException could be thrown anywhere. Ouch. You can't catch it in the calling thread either, after all that thread just continues immediately after the call to sleep().
The sleep() method only makes good sense when applied to the current thread; the fact that it is a static method in Thread enforces this.
- Peter

[This message has been edited by Peter den Haan (edited October 10, 2001).]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!