06 April 2011

Java Proficiency - final

What does the keyword final do in this code block?
1:  public class MyClass { 2:     public final int doStuff() {} 3:  }
Methods declared as final within a class cannot be overridden by a subclass. So if we extend MyClass we can't be messin' with doStuff(). It's final.

What if the class is declared as final?
1:  public final class MyClass { 2:     public int doStuff() {} 3:  }
Declaring a class as final means that class cannot be extended.

These short answers are not meant to be a definition or a tutorial in the use of final. They are meant to give the answer the technical interviewer seems to want.

0 comments: