abstract method in interface

Hi!
In java.lang.Runable there is an abstract method: run().
Isn't it obvious that interface contains only abstract methods?
Is there a reason to that decleration?
And talking about thread, if i extends Tread, i must implement the run method although Thread implements it. why is that?
Thank you!
rzvika3Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rzvika3Author Commented:
Edited text of question.
0
Ravindra76Commented:


1. Interface contains abstract methods only.

all methods in the interface are default abstract methods.

2./**
     * If this thread was constructed using a separate
     * <code>Runnable</code> run object, then that
     * <code>Runnable</code> object's <code>run</code> method is called;
     * otherwise, this method does nothing and returns.
     * <p>
     * Subclasses of <code>Thread</code> should override this method.
     *
     * @see     java.lang.Thread#start()
     * @see     java.lang.Thread#stop()
     * @see     java.lang.Thread#Thread(java.lang.ThreadGroup, java.lang.Runnable, java.lang.String)
     * @see     java.lang.Runnable#run()
     * @since   JDK1.0
     */
    public void run() {
      if (target != null) {
          target.run();
      }
    }

 where target is

private Runnable target;

Note:

It is not necessary that extendig thread means you have to provide run() method. But it is usual way to provide run() method.

Eg : the Thread which does not provide run() and compiled.


public class X extends Thread {


      public  X() {
            super();
      }
      public static void main(String args[]) {

            X x = new X();
      }

}

If you observe the above class, it does not have run method. If you provide, it will run when thread was started.




But if you implements Runnable, you must provide run() method.

Since Runnable is interface, you must provide implementation,


public class X implements Runnable {


      public  X() {
            super();
      }
      public static void main(String args[]) {

            X x = new X();
      }

}

won't compile till you implement it.

If Thread is abstrat class with run as abstract method, then my above code of class X extends Thread won't compile.


Summary:

 Runnable is interface. You must provide implementation for run() method.


Thread is neither abstrat class with run() as abstract method nor it is interface.

You may skip run() and compiler will compile.

If you give run() method by extending thread, it will run

Best of luck





0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shaveriCommented:
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.
0
Ravindra76Commented:

The Runnable interface is provided to solve java single inheritance thread problem.

IF an object extends Thread menas it won't extend another class due to multiple inheritance.

THat's why Runnable is introduced so that a class which may extends some class and still it be a thread just be implementing Runnable interface.

Summary:

If you want a class to be thread:

A. If this class is not extending any class, use extends Thread.

B.If your class is inheriting any another class use Runnable interface.

Best of luck

 
0
rzvika3Author Commented:
Thank you!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.