Nil Nil - 6 months ago 16
Java Question

why abstract property is lost in a subclass of Runnable interface?

I am actually learning Threads. But get stuck with a very basic problem. I know if I inherit an interface in my class I have to give definitions of all the methods (mentioned in that interface) in my inherited class, else it has to be declared as abstract. Now I have tried very simple code...

import java.util.*;
import java.lang.*;
import java.io.*;


abstract class Code implements Runnable
{
public static void main (String[] args) throws java.lang.Exception
{

}
}


there was no problem in the compilation,and then I removed the
abstract
keyword from class code and it was showing me compilation error,saying class code is not abstract...

There has to be error. But then I did what ,checking some variation,extended Thread class also from class and recompiled the code. And I got no compilation error! Though I have not given the definition of
run()
method, neither I mentioned my class as abstract.
there...

class Code extends Thread implements Runnable
{
public static void main (String[] args) throws java.lang.Exception
{

}
}


means this class lost the abstract property. How was that possible? Anyone explain please.

Answer

Well,you know Runnable interface having only one abstract method. i.e,

abstract public void run();

If you are implementing Runnable you have to provide definition of run() method in your own concrete class or you can make your class abstract. Now you can provide the definition of run() method in your class explicitly,or implicitely by inheriting any other class having a valid definition of run() method. Thread class also has a valid definition of run() method. So, if you are inheriting both Thread class and Runnable interface in your class,that class is no longer carrying any abstract property.that is why you did not get compilation error.Make sure about the access specifier of run() method. If you are inheriting any other class instead of Thread you have to provide there concrete run() method as,

public void run()
 {
    //body
 }