I have three classes as follows :
1.ThreadInstance
public class ThreadInstance extends Thread {
public ThreadInstance()
{
}
public ThreadInstance(RunnableInstance r) {
super(r);
}
public void run() {
System.out.println("|Extend|");
}
}
2.RunnableInstance
public class RunnableInstance implements Runnable {
public void run() {
System.out.println("|Implement|");
}
}
3.Client
public class Client{
public static void main(String[] args) {
new ThreadInstance(new RunnableInstance()).start();
}
}
OUTPUT prints |Extend| instead of |Implement|
In Client class, a thread is created by passing a runnable instance. So when this thread run, run() method of Runnable class(RunnableInstance) should be called. But in the above case, run() method of ThreadInstance is called.
I am not bale to get the reason behind this. Could anyone please put some light on what am i missing here. Also i checked the run() method of Thread class and found that if checks if runnable instance is not null, then it calls run() method from Runnable class.