Interview Question and Answers on final keyword in Java

In this article, we will cover some of the interview questions with their justification on final keyword in Java

These are most frequently asked interview question from OOPS concepts

 

Read final keyword in Java concepts in detail

 

Q) What is the use of final keyword in Java?

–> final keyword can be used with variables, methods or classes

–> final variables cannot be changed, once its initialized

–> final methods cannot be overridden although it can be invoked

–> final classes cannot be inherited

 

Q) Where all final keyword can be applied?

–> final keyword can be applied with variables, methods or classes

 

Q) What is blank final variable?

–> A final variable declared without initializing value is known as blank final field or blank final variable

 

Q) What is static blank final variable?

–> A static final variable declared without initializing value is known as static blank final field or static blank final variable

 

Q) What happens, if final variable is NOT initialized with initial value at the time of declaration?

–> Compilation error will be thrown

–> Compile-time error: The blank final field <final_variable> may not have been initialized

 

Q) What happens, if final variable is re-assigned in the same class body?

–> Compilation error will be thrown

–> Compile-time error: The final field <final_variable> cannot be assigned

 

Q) Explain order of final variable initialization in Java?

–> The value of final variable can be initialized at three places (considering it’s an instance data member)

  • Initialize where it is declared
  • Inside instance blocks i.e.; {curly braces}
  • Constructor

–>  final variable checks whether value is initialized in above order and if it doesn’t find value is initialized then compiler throws error

 

Q) What happens, if final method is overridden in the sub class?

–> Compilation error will be thrown by compiler

–> Compile-time error: Cannot override the final method from <Parent_Class>

 

Q) Whether it is possible to invoke final methods?

–> Yes absolutely, we can very well invoke final method from inherited sub class

 

Q) Whether it is possible to inherit final class? And what happens, if we extend final class?

–> A final class cannot be inherited

–> Still, if we try to inherit then compilation error will be thrown by compiler

–> Compile-time error: The type <Child_Class> cannot subclass the final class <Parent_Class>

 

Q) Can we declare interface as final? If not, why explain?

–> An interface cannot be declared as final

–> If we declared interface as final, then compilation error will be thrown by compiler

–> Compile-time error: Illegal modifier for the interface TestInterface; only public & abstract are permitted

8_Interview_question_final_keyword_final_interface_error

–> Reason: As interface contains only abstract methods, marking interface as final will restrict the interface to be inherited (and provide implementation for abstract methods)

 

Q) Can we declare constructor as final?

–> Compilation error will be thrown by marking/declaring constructor as final

–> Compile-time error: Illegal modifier for the constructor in type Employee; only public, protected & private are permitted

9_Interview_question_final_keyword_final_constructor_error

 

Q) Whether final keyword allowed in input parameters (formal parameters) of method signature?

–> Yes it is allowed to declare final in method parameters, but final parameters cannot be changed

 

Q) Whether compilation succeeds, if instance of final class is created?

–> Yes, object of final class can be created; only it cannot be inherited

 

Q) Difference between abstract method and final method?

–> Abstract methods needs to be implemented; therefore it is overridden in the sub class

–> Whereas final methods cannot be overridden in the sub class

–> So conceptually final methods and abstract methods are opposite to each other where one needs to be overridden and other restrict to be overridden

 

Q) Relationship between immutable class and final class?

–> final class is a way to create immutable class in Java

–> Example for final from Java library

  • String and Math classes from java.lang package
  • getClass(), notify(), notifyAll(), wait() are final methods from java.lang.Object class
  • PI and Math.E are final variable in Math class

 

References:

https://docs.oracle.com/javase/tutorial/java/IandI/final.html

 

Read Also:

Happy Coding !!
Happy Learning !!