Tuesday, 28 July 2009

Java Certification Question 0047

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

class Base{

Base(){

System.out.println("Base");

}

}

public class Checket extends Base{

public static void main(String argv[]){

Checket c = new Checket();

super();

}

Checket(){

System.out.println("Checket");

}

}

1) Compile time error
2) Checket followed by Base
3) Base followed by Checket
4) runtime error


Answer 1:

1) Compile time error

With the sun JDK it will produce the following error

"Only constructors can invoke constructors".

If you took out the call to super that causes this error the program would compile and at runtime it would output Base and then Checket as constructors are called from the oldest ancestor class downwards.

No comments: