Final Keyword in Java
The keyword final in Java can be used for restriction of usage of variables,
methods, and classes.
Final Variables: When we define a variable with the final keyword then we can’t
change its value.
Final Class: If we want to restrict a class to be inherited (extend) then
we use the final keyword.
Final Method: We can’t override the method if we define the method with the final.
Java final variable
Once we define a variable with the final
keyword, that variable can be reassigned and become constant.
public class FinalVariable
{
public static void main(String
args[])
{
final int number = 10;
System.out.println(number);
number = 20; // Here we reassign a final variable,
//but it will give an error
System.out.println(number);
}
}
In the above program, we are trying
to change number variable to 20 but it will give a compile-time error due final
keyword declaration.
What is Blank Final Variable in Java?
The final variable without initial value
is called the Blank Final variable.
class BlankFinalVariable
{
final int number; // final instance variable.
{
number = 50; // Initialization in the constructor
}
}
Java final method
When we don’t want to override a
method we use the final keyword. It means we cannot override in a subclass. Here is
an example to explain the concept…
public class FinalMethodExample
{
final void display()
{
System.out.println("I am from Base class
method/ function");
}
}
public class Test extends
FinalMethodExample
{
void display()
{
System.out.println("I am from Drive
class method/ function");
}
public static void main(String args[])
{
Test ob=new Test();
ob.display();
}
}
As in base class display ()
method/function is defined as a final method so it will not be overridden in derived
class. We try to compile Test class it will give an error as
“display () in Test cannot override
display() in FinalMethodExample overridden method is final”.
Java final class
If we define a class with the final
keyword then it cannot be inherited. In other words, we
cannot extend a final class.
Before the final keyword, the program runs
fine. In the following program, BaseClass inherited by BaseClass without any
error and output is Hello World
{
void display()
{
System.out.println("Hello
World");
}
}
public class DriveClass extends
BaseClass
{
public static void main(String args[])
{
DriveClass ob=new DriveClass();
ob.display();
}
}
Output
Hello World
But after placing the final keyword in the final BaseClass then it will give an error
"cannot inherit from final BaseClass
"
Interview Question and Answer of final keyword in java
What is the blank final field?
An uninitialized variable with final the keyword is called blank final field.
Can we initialize blank final variable?
Yes, and it is only in the constructor.
What is the use of the final class?
When we don’t want to inherit a
class or extends a class then we use the final. It is a security mechanism.
Is it possible to declare constructors as final?
No, it is not possible a constructors
be as final.
What is the usage of the final keyword in Java?
Final as a variable: We cannot
reassign a final variable.
Final as a method: Final method or the function can be overridden.
Final as a class: Final class cannot
be inherit.
Is it possible a main() method be declared as final?
Absolutely Yes, the main() method
can be assigned as final and cannot be overridden.
More tutorial and examples of java
Loops in Java - Java loop explained with examples