Pages

Program to implement Runnable interface

// Program to implement Runnable interface
class A
{
 void fun1()
 {
 System.out.println("Inside fun1");
 }
 void fun2()
 {
 System.out.println("Inside fun2");
 }
}
class B extends A implements Runnable
{
 public void run()
 {
 for(int i=0;i<10;i++)
 System.out.println("Inside B " + i);
 }
}
class RunnableDemo
{
public static void main(String args[])
{
 B b1= new B();
 b1.fun1();
 b1.fun2();
 Thread t=new Thread(b1);
 t.start();
 for(int i=0;i<5;i++)
 System.out.println("Inside main");
 }
}

-> In the above program, because Runnable interface does not have start() method, the object of Thread B is passed as argument to the constructor of Thread class and through the Thread class object, we are calling start() method [t.start()].