Final Keyword Interview Questions in Java
Interview Questions on Final Keyword in Java
Where you use final keyword in java ?
Final keyword in java are used at;
- Final at method level
- Final at class level
- Final at variable level
What is the use of the final keyword in Java?
Final keyword can be applied to variable, method, and class. Each of them has its own uses
What are final class, final method and final variable ?
- Final class: can not be extended.
- Final method: can not be overridden in the sub class.
- Final class: can not change it's value once it is initialized.
What is Final Variable ?
The final variable is a variable whose value cannot be changed at any time once assigned, it remains as a constant forever.
What is Final Method ?
The final method cannot be overridden
What is Final Class ?
A final class cannot be subclassed (cannot be extended)
What is a blank final variable ?
A blank final variable is a final variable, which is not initialized during declaration.
What is the main difference between abstract method and final method?
Abstract methods must be overridden in sub class where as final methods can not be overridden in sub class
When use final class in java ?
If a class needs some security and it should not participate in inheritance in this situation we need to use final class.
What will happen if we try to extend final class in java ?
Compiler get an error
Can we declare final variable without initialization ?
Yes, We can declare a final variable without initialization and these final variables are called a blank final variable but must be initialized before usage.
Can we declare interface as final ?
No, because interface must be implement in other class.
Can we declare constructor as final ?
No, Constructor cannot be declared as final. Constructors are not inherited and so it cannot be overridden, so there is no use to have a final constructor.
You will get an error like "Illegal modifier for the constructor in type Test; only public, protected & private are permitted"
Can a main() method be declared final ?
Yes, the main() method can be declared as final and cannot be overridden.
Can we declare constructor as final ?
No, cunstructor can't be final.
What will happen if we try to override final methods in sub classes ?
Compile time error will come :Cannot override the final method from Super class, because you can't override final method in sub class.
Can we create object for final class?
Yes we can create object for final class.
Can we declare constructors as final?
No, constructors can not be final.
Can we make a method final in Java ?
Yes, We can make a method final, the only constraint is that it cannot be overridden.
Can we make the local variable be final ?
Yes, we can make a local variable final in Java. In fact, the final is the only modifier which can be applied to a local variable. If we apply any other modifier we will be getting compile time error [only final is permitted].
Can we declare an interface as final ?
The sole purpose of Interface is to have the subclass implement it if we make it final it cannot be implemented. Only public & abstract are permitted while creating an interface.
Can Final Variable be serialized in Java ?
Yes, the final variable can be serialized in Java
What is the use of final keyword in java ?
Final keyword in java is used to make any class or a method or a field as unchangeable. We can't extend final class, not override final method, can't change value of final variable.
Can we change the value of an interface field ?
No, we can't change the value of an interface field. Because interface fields is by default final and static. They remain constant for whole execution of a program.
Can final method be overloaded in Java ?
Yes, the final method can be overloaded but cannot be overridden. Which means you can have more than one final method with the same name with different parameters.
Can we create object for final class ?
Yes, it is possible to create an object for a final class. The best example in Java would be String class. The string is a final class, in almost all code we will be creating the object for it but you cannot extend the String class.
What is the main difference between abstract methods and final methods ?
Abstract methods are declared in abstract classes and cannot be implemented in the same class. They must be implemented in the subclass. The only way to use an abstract method is by overriding it Final methods are quite opposite to abstract, final methods cannot be overridden.
What is the difference between abstract class and final class ?
ABSTRACT CLASS | FINAL CLASS | |
---|---|---|
1 | Abstract class can be subclassed and the abstract methods should be overridden | Final class cannot be subclassed and the final methods cannot be overridden |
2 | Can contain abstract methods. | Cannot contain abstract methods. |
3 | Abstract class can be inherited. | Final class cannot be inherited. |
4 | Abstract class cannot be instantiated. | Final class can be instantiated. |
5 | Immutable object cannot be created. | Immutable objects can be created. |
6 | Not all methods of the abstract class need to have a method body (abstract methods). | All methods of the final class should have a method body. |
Can we use non-final local variables inside a local inner class ?
No. Only final local variables can be used inside a local inner class.
Can we change the value of an interface field? If not, why ?
No, we can’t change the value of an interface field. Because interface fields, by default, are final and static. They remain constant for whole execution of a program.
What is the difference between static and final in Java ?
ABSTRACT CLASS | FINAL CLASS | |
---|---|---|
1 | Static keyword can be applied to a nested class, block, method and variables | Final keyword can be applied to class, block, method and variables |
2 | We can declare static methods with the same signature in subclass but it is not considered as overriding as there won’t be any runtime polymorphism.
If a subclass contains the same signature as a static method in the base class, then the method of the subclass hides the base class method it is called Method Hiding. | Final class methods cannot be overridden. |
3 | Static variable can be changed after initialization | Final variable cannot be changed after initialization |
4 | Static method or variable can be accessed directly by the class name and doesn’t need any object as they belong to the class | Object can be created to call the final method or final variables |
What is a Static Final variable in Java ?
When have declared a variable as static final then the variable becomes a CONSTANT. Only one copy of variable exists which cannot be changed by any instance.