Final Keyword Interview Questions

What is the use of final keyword in java?

By using final keyword we can make,

  • Final class.

  • Final method.

  • Final variables.

  • If we declare any class as final we can not extend that class.

  • If we declare any method as final it can not be overridden in sub class.

  • If we declare any variable as final its value unchangeable once assigned.

Learn about Final Keyword in Detail

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.

What is the actual use of final class in java?
  • If a class needs some security and it should not participate in inheritance in this scenario we need to use final class.

  • We can not extend final class.

Can we declare interface as final?

No, we cannot declare interface as final because interface should be implemented by some class so its not possible to declare interface as final.

Final Keyword Example

Is it possible to declare final variables without initialization?
  • No. It's not possible to declare a final variable without initial value assigned.

  • While declaring itself we need to initialize some value and that value cannot be change at any time.

Final Keyword Example

Final Keyword Example

Can we declare constructor as final?

No. Constructors can not 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.

Can we create object for final class?

Yes we can create object for final class.

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.

You can’t extend a final class, you can’t override a final method and you can’t change the value of a final field.

Final keyword is used to achieve high level of security while coding.

What is the blank final field?

Uninitialized final field is called blank final field.

When do you override hashcode and equals() ?

Whenever necessary especially if you want to do equality check or want to use your object as key in HashMap.

Can we change the state of an object to which a final reference variable is pointing?

Yes, we can change the state of an object to which a final reference variable is pointing, but we can’t re-assign a new object to this final reference variable.

What is the main difference between abstract methods and final methods?

Abstract methods must be overridden in the sub classes and final methods are not at all eligible for overriding.

What is the use of final class?

A final class is very useful when you want a high level of security in your application. If you don’t want inheritance of a particular class, due to security reasons, then you can declare that class as a final.

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 are final class, final method and final variable?
  • final class —> cannot be extended.

  • final method —> cannot be overridden in the sub class.

  • final variable —> cannot change it’s value once it is initialized.

Learn more about Java by our Experts

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 declare constructors as final?

No, constructors cannot be final.