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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Ravindra76Connect With a Mentor Commented:


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
 
rzvika3Author Commented:
Edited text of question.
0
 
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
All Courses

From novice to tech pro — start learning today.