Java Final Method with Example

A final method cannot be overridden by any subclass in java. This means if we try to override final method through subclass compiler will throw an error. 
This is example of Java final method
public class FinalMethod {
  public final void finalMethodClass()
          System.out.println("This is final method of java");
Cannot override final methods
public class SubClassFinalMethod extends FinalMethod{

        public void finalMethodClass()
                // Error by compiler
                // Cannot override the final method from FinalMethod
This will throw error as shown in code

Final Classes in Java

The class declared as final can't be subclass or extend..
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.

Blank final

The blank final, which was introduced in Java 1.1, is a final variable whose declaration lacks an initializer.  A blank final can only be assigned once and must be unassigned when an assignment occurs. In order to do this, a Java compiler runs a flow analysis to ensure that, for every assignment to a blank final variable, the variable is definitely unassigned before the assignment; otherwise a compile-time error occurs.

In general, a Java compiler will ensure that the blank final is not used until it is assigned a value and that once assigned a value, the now final variable cannot be reassigned another value

People who read this post also read :


Post a Comment


Twitter Delicious Facebook Digg Stumbleupon Favorites More