Pages

Final Keyword Interview Questions in Java



Final Keyword Interview Questions in Java

Final keyword in java is one of the interview cracking topic, lots of questions are ask in any java interview. So on this behalf today we discuss only Final Keyword Interview Questions 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. 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.

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.

Where all we can initialize a final non-static global variable if it is not initialized at the time of declaration?

In all constructors or in any one of instance initialization blocks.

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.

Where all we can initialize a final static global variable if it is not initialized at the time of declaration?

In any one of static initialization blocks.

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.

Is final method inherited?

Yes, final method is inherited but you cannot override it.

What is blank or uninitialized final variable?

A final variable that is not initialized at the time of declaration is known as blank final variable.

Can we initialize blank final variable?

Yes, but only in constructor.

What is final parameter?

If you declare any parameter as final, you cannot change the value of it.

Can we declare a constructor final?

No, because constructor is never inherited.

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.

What will happen if we try to extend final class in java?

Compile time error will come. 

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 most common predefined final class object you used in your code? 

String (for example)




No comments:

Post a Comment

 

Most Reading