Monday, April 11, 2011

Final Classes in Java(tm)

Final Classes in Java(tm)

In Java, a class organization such as:

class A {}

class B extends A {}

results in a superclass (A) and a subclass (B). References to B objects may be assigned to A references, and if an A reference "really" refers to a B, then B's methods will be called in preference to A's. All of this is a standard part of the object-oriented programming paradigm offered by Java.

But there is a way to modify this type of organization, by declaring a class to be final. If I say:

final class A {}

then that means that A cannot be further extended or subclassed.

This feature has a couple of big implications. One is that it allows control over a class, so that no one can subclass the class and possibly introduce anomalous behavior. For example, java.lang.String is a final class. This means, for example, that I can't subclass String and provide my own length() method that does something very different from returning the string length.

There is also a big performance issue with final classes. If a class is final, then all of its methods are implicitly final as well, that is, the method is guaranteed not be overridden in any subclass. A Java compiler may be able to inline a final method. For example, this program:

final class A {
private int type;
public int getType() {return type;}
}

public class test {
public static void main(String args[])
{
int N = 5000000;
int i = N;
int t = 0;
A aref = new A();
while (i-- > 0)
t = aref.getType();
}
}

runs about twice as fast when the class is declared final.

Of course, much of the time it's desirable to use the superclass / subclass paradigm to the full, and not worry about wringing out the last bit of speed. But sometimes you have heavily used methods that you'd like to have expanded inline, and a final class is one way of achieving that.

123passportphoto is a very easy to use passport photo website that provides six enhanced photos. I have never had an issue while using this ...