News

Tutorial: The Keyword “Final” in Java

Program for Final Variable

By this time, i hope you have become conversant with the keywords like static and this in Java. In Java programing language ‘final’ is a keyword that is used for different purposes and here is list of all contexts where we use final:

  • Final variables can’t be modified rather, can’t be re-initialized.
  • Final methods can not be overridden.
  • Final classes can’t be inherited.

Now we will understand all these, using the following examples for each of the contexts:

Programs

1. Program for Final Variable

public class Circle{
  public static void main(String args[]){
    final double PI = 3.14;
    final double radius = 3.50;
    System.out.println("Area of Circle is: "+(PI*Math.pow(radius,2)));
    radius = 3.75;
    System.out.println("Area of Circle is: "+(PI*Math.pow(radius,2)));
  }
}

Program for Final Variable

2. Program for Final Method

public class Figure {
  final public double calculateArea(double radius){
    return Math.pow(radius,2);
  }
}
class Circle extends Figure{
  public double  calculateArea(double radius){
    System.out.println("Overriding Not Allowed");    /*  calculateArea() can not be overridden */
  }
}
class Main{
  public static void main(String args[]){
    Circle circle = new Circle();
    circle.calculateArea(1.23);
  }
}

Program for Final Method

3. Program for Final Class

final class Circle{
  final double PI = 3.14;
  double calculateArea(double radius){
    return (PI*Math.pow(radius,2));
  }
}
  class Cylinder extends Circle{
    double height = 10.0;
    double calculateArea(double radius){
    return 2*((PI*Math.pow(radius,2))+(PI*radius*height));
  }
}
class Main{
  public static void main(String args[]){
    Cylinder cylinder = new Cylinder();
    cylinder.calculateArea(3.11);
  }
}

Program for Final Class

Explanation

If variables are declared as final using final keyword, it can be initialized only once and generally it is defined during declaration. If it is not initialized during declaration, so called final blank variable has to be defined within the constructors otherwise it will get compile-time error.

Once it is defined, it can be re-initialized again. If initialized, then there will be compilation error. From the output of the first program it can be easily made out.

If methods are declared as final using final keyword in a class, it can be overloaded but can’t be overridden. In the example given above, we have defined final calculateArea() in Figure class, couldn’t be inherited and overridden in class Circle class as it gives the compile-time error.

Declaring methods as final is equivalent to private method but for variable it is not true and final variable is not at all equivalent to constant variable as while declaring array as final, the state of the array object can be modified i.e. mutable so in order to restrict modifications, it has to be made immutable but when an array is declared as constant, it can’t be modified anyway.

To prevent the inheritance of a class, the concerned class is declared as final. Here in the third program, we tried to inherit the final class but we got compilation error saying “cannot inherit from final Circle”, can be observed from the screen-shot while compilation.

So here final is discussed thoroughly and I think it will help you a lot to understand final in Java and all its usages.

1 Comment

1 Comment

  1. Juliosaituamba

    April 11, 2012 at 3:06 PM

    very helpfull like it 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top