Final Keyword In Java
The final is a keyword. This is similar to the const keyword in other languages. This keyword may not be used as identifiers i.e. you cannot declare a variable or class with this name in your Java program.
The final keyword in java is used to restrict the user. The final keyword can be used in many contexts. Final can be:
- Variable
- Method
- Class
The final keyword can be applied with the variables, that have no value it is called blank final variable. It can be initialized in the constructor only. The blank final variable can be static also which will be initialized in the static block only. We will have a detailed learning of these. Let's first learn the basics of the final keyword.
final variable
If you specify any variable as final, you can not change its value if once assigned. It will be constant.
Example to understand final variable
class Counter
{
public static void main(String ar[])
{
final int count=1;//specify count as final
count=count+1;//will give error, can not be changed value of count
System.out.println(count);
}
}
Output :
Counter.java:6: error: cannot assign a value to final variable count
count = count + 1; //will give error, can not be changed value of count
Compile Time Error:
Here compile time error occurred in output because the final count variable's value can not be changed. and we tried to change it.
final method
If you specify any method as final, you can not override it.
Example to understand the final method
class Vehicle {
final void run()// specified run() as final
{
System.out.println("vehicle running");
}
}
class Car extends Vehicle {
void run()// trying to override final run() method
{
System.out.println("Car running");
}
public static void main(String args[])
{
Car c = new Car();
c.run();
}
}
Output:Car.java:11: error: run() in Car cannot override run() in Vehicle
void run() //trying to override final run() method
^
overridden method is final
In the above program, a compile-time error occurred because final run() of Vehicle class cannot be overridden. and we tried to override it.
final class
If you specify any class as final, you can not inherit/extend it.
Example to understand final class
final class Vehicle// specified Vehicle as final class
{
}
class Car extends Vehicle// trying to extend Vehicle class
{
void run()
{
System.out.println("Car running");
}
public static void main(String args[]) {
Car c = new Car();
c.run();
}
}
Output:Car.java:5: error: cannot inherit from final Vehicle
class Car extends Vehicle //trying to extend Vehicle class
Question :
1. What is blank or uninitialized final variable?
A final variable which is not initialized at the declaration time, is known as blank final variable. If you do not initializefinal Instance variable at declaration time, then you will have only one option to initialize it in Constructor. If you do not initialize final static variableat declaration time, then you will have only one option to initialize it in static block.
class Test
{
static final int s;// did not initialize final static variable s
final int i;// did not initialize final instance variable i
static {
s = 5;// now s can be initialized in only static block
}
public Test()
{
i = 10;// now i can be initialized in only constructor
}
public static void main(Strin gar[])
{
Test t = new Test();
System.out.println("s = " + t.s);// can write also Test.s except of t.s
System.out.println("i = " + t.i);
}
}
Output :
s = 5
i = 10
2. Is final method inherited?
Yes, final method is inherited, only you can not override it.
3. Can we declare a constructor as final?
No, constructor is not inherited so no any need to make it final.
Java Interview Questions On final Keyword
- What is the Use of final class in Java?
- What is the use of the final keyword in Java?
o What is the final class in Java?
o Can we declare object as final?
o Can we change value of final variable in Java?
o Can we declare main method as final?
o Can we overload final method in Java?
o Can we extend final class in Java?
o Can we declare constructor as final
o Can we create abstract and final class in Java?
0 comments:
Post a Comment