Warning: mysqli_connect(): (HY000/2002): Connection refused in /home/programc/public_html/wp-content/plugins/wp-pro-quiz/wp-pro-quiz.php on line 38

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/programc/public_html/wp-content/plugins/wp-pro-quiz/wp-pro-quiz.php on line 40

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given in /home/programc/public_html/wp-content/plugins/wp-pro-quiz/wp-pro-quiz.php on line 41

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/programc/public_html/wp-content/plugins/wp-pro-quiz/wp-pro-quiz.php on line 45

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /home/programc/public_html/wp-content/plugins/wp-pro-quiz/wp-pro-quiz.php on line 48

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/programc/public_html/wp-content/plugins/wp-pro-quiz/wp-pro-quiz.php on line 49
How to use final keyword in Java - Get Program Code
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.

final class Final_class    //  class is defined as final class *
{   
void Function1() 
{  
System.out.println("Hello this function is executing");
}

void Function2()  
{
System.out.println("Hello this second function is executing");
}

}  // end of class

class Final_Inherit extends Final_class   //  inheriting the final class **
{
public static void main(String args[])   //  main function
{
Final_Inherit obj=new Final_Inherit();   //  object  is created 
obj.Function1();   // calling the function
obj.Function2();  // calling the function
}  // end of main
}  // end of class

*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.

class Final_method 
{
void Function1()
{
System.out.println("This is simple function");
}

final void Function2()  // declaring the method final so that which cannot be overriden
{
System.out.println("Final function cannot be overwritten");
}

}
class Final_inherit extends Final_method
{
void Function2()
{
System.out.println("This is override function of final class");
}
public static void main(String args[])
{
Final_inherit obj=new Final_inherit();
obj.Function1();
obj.Function2();  //   *see below
}
}

* 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.

class Final_variable
{
public static void main(String args[])
{
final int x=10;
x=x+5;  //  cannot assign value to the final variable
System.out.println("Printing the value "+x);  // x remains constant as we define at the time of declaration
}
}

Tags: final keyword in java

 

Check Also

Program to check Upper Triangular matrix

What is Upper Triangular Matrix?  An upper triangular matrix is a square matrix in which ...

Leave a Reply

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

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