Java Final Keyword-interview
Q1. Can a final variable be initialized later?
Yes, but it must be initialized once either:
- At the time of declaration.
- Inside the constructor (for instance variables).
Q2. Can a final method be inherited?
Yes. A final method can be inherited but cannot be overridden in the subclass.
Q3. Can we make an abstract class final in Java? Why/Why not?
No.
- Abstract class must be inherited to provide implementation.
- Final class cannot be inherited.
Q4. What is the difference between final, finally, and finalize?
- final → Keyword used to restrict variables, methods, or classes.
- finally → Block used in exception handling, executed always.
- finalize() → Method called by garbage collector before object destruction.
Q5. What is the final keyword in Java?
The final keyword is used to restrict the user:
- Final variable → Value cannot be changed after initialization.
- Final method → Cannot be overridden by subclasses.
- Final class → Cannot be subclassed.