• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface questions

 
Dennis Tribby
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In preparing for the SCJP I have certain doubts regarding interfaces.
Given that an interface can only be extend other interfaces and since it can only contain abstract methods (which have no method body), why bother with them at all?
From the code listed below couldn't we ignore the "implements Runnable" and replace the "public void run()" with "public void myCompute". Since the run(), for example, doesn't do anything I couldn't I substituted my own code? From within the body of an interface can anything else be added beyon abstract methods?
The following code snippet is from Sun's SDK 1.3 API documentation
class PrimeRun implements Runnable {
long minPrime;
PrimeRun(long minPrime) {
this.minPrime = minPrime;
}

public void run() {
// compute primes larger than minPrime
. . .
}
}

I'm guessing that the Runnable (et.al.) interface has some special meaning at the compiler level.. If so, what?
Thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
hi dennis,
interface is like a skeleton..it can contain non abstract methods also..In ur example, the Runnable interface allows u to create a thread in your class and you need to override the run() method of the Runnable interface...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic