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 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 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 interface as final ?
No, because interface must be implement in other class.
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.
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.