Sunday, 16 August 2009

Java Certification Question 0066

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

class Base{

public final void amethod(){

System.out.println("amethod");

}

}

public class Fin extends Base{

public static void main(String argv[]){

Base b = new Base();

b.amethod();

}

}

1) Compile time error indicating that a class with any final methods must be declared final itself
2) Compile time error indicating that you cannot inherit from a class with final methods
3) Run time error indicating that Base is not defined as final
4) Success in compilation and output of "amethod" at run time.



Answer 4) Success in compilation and output of "amethod" at run time.

A final method cannot be ovverriden in a sub class, but apart from that it does not cause any other restrictions.

No comments:

Post a Comment