Wednesday, 15 July 2009

Java Certification Question 0035

What will happen when you attempt to compile and run the following code

public class Borley extends Thread{

public static void main(String argv[]){
Borley b = new Borley();
b.start();
}

public void run(){
System.out.println("Running");
}
}

1) Compilation and run but no output
2) Compilation and run with the output "Running"
3) Compile time error with complaint of no Thread target
4) Compile time error with complaint of no access to Thread package



Answer :
2) Compilation and run with the output "Running"
This is perfectly legitimate if useless sample of creating an instnace of a Thread and causing its run method to execute via a call to the start method. The Thread class is part of the core java.lang package and does not need any explicit import statement. The reference to a Thread target is an attempt to mislead with a reference to the method of using the Runnable interface instead of simply inheriting from the Thread super class.

No comments: