Sunday , January 22 2017
Home / JAVA / How to use final keyword in Java

How to use final keyword in Java

Many queries related how to use final keyword came across. In java we can use final keyword in three ways:

1-Declare the class as final.
2-Declare the method as final
3-Declare the variables as final.

Final class

To declare a class as “Final” means that a class cannot be inherited by any other class,instead it can be instantiated by making an object and execute the method defined in that class only. By making a class final we can ensure the security of data and behavior used in the class so that no one could use the properties of the object defined in the final class. Final class has the characteristic opposite of the abstract as we have have discussed in our previous tutorial of abstract class. Also,we cannot extend the final class,if we try to do it we receive an error after compilation that final class cannot be inherited.

*means we cannot inherit the final class but only object can be created.

**but this statement create’s the error after the compilation as we cannot inherit the final class.

Final Method

By declaring method as final ,it means that method defined final cannot be overridden in another class after inheriting the properties of the parent class.

Final methods are those methods which remains same as it is in the parent class after inheriting the properties of parent class in a child class.

* After calling the function2 which is final method in the parent class cannot be overridden in the child class.After compilation it creates an error that method declared as final cannot be overridden.

Final Variables

The variables declared as final remains constant in the whole program.It means that after the final declaration of the variables ,if we change the value of that variable by logical implementation ,than it’s value could not be change.If we call that variable and printing it than we remain the constant value defined at the time of declaration of that variable.

Tags: final keyword in java


Also See:   Java program to change lower case to upper case and vice versa

Check Also

Program for Matrix Multiplication in JAVA

Matrix Multiplication:– Here is given a Java Program for Matrix Multiplication also call matrix product. In ...

Leave a Reply

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

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.