Thursday, July 22, 2021

Top 21 Java Final Modifier (Keyword) Interview Questions and Answers

The final modifier is one of the important keywords in Java. You can use this with a class, method, and variable as well. Good knowledge of the final keyword is not only essential for writing good, performant, and secure Java programs but also to clear any  Java interview. The interviewer pays special attention to the final keyword and expects candidates to know how and when to use the final modifier with variables, methods, and classes. In this article, I have collected Java interview questions that are based upon the concept of the final modifier. You might have seen many of these questions on both telephonic and face-to-face rounds. 

You can use this collection not only to check your knowledge about the final keyword but also to prepare for the final modifier from the Java interview's perspective.  If you have come across any interesting final modifier question which is not in this list, please share with us as a comment and I'll add to this list.



21 Java final modifier Interview Questions

So, what are we waiting for? let's start with the first question in this list:


1. What is the use of the final keyword in Java?
The final keyword can be used with a class, method, and variables. If it is used with class then it prevents inheritance by not allowing you to create subclasses. If it is used with methods then it prevents overriding, you cannot override a final method in Java. If it is used with variables then they are treated as constant because you cannot change their value once assigned. See here to learn more about the final modifier in Java.




2. Can we make a variable final in Java? What is different between a normal variable and final variable?
Yes, you can make a variable final in Java. The difference between normal variable and final variable comes from multiple assignments. you can re-assign value to a normal variable but you cannot change the value of a final variable once assigned. See here to learn more about when to make a variable final in Java.


3) Can we make a method final in Java? explain the difference between normal method and final method
Yes, you can make a method final in Java. The difference is in the fact that you can override a non-final method, provided it'st not private and static but you cannot override a final method in Java. See here to learn more about when to make a method final in Java.


4) Can we make local variable final in Java?
Yes, you can make local variable final in Java. In fact, this was mandatory, if you want to access the local variable inside an Anonymous class until Java 8. From Java 8 onward, you don't need to make it final but make sure you don't change the value once assigned. This is also known as an effectively final variable in Java. See Java SE 8 for Really Impatient by Cay S. Horstmann to learn more about the effectively final variable in Java.

Java Final Keyword Interview Questions and Answers


5) What is a blank final field or variable in Java?
Java allows you to create a final member variable without assignment but requires you to assign the value in either static initializer block, if member variable is static or inside every constructor, if member variable is non-static. The final variable without assignment or value is called blank final variable, the compiler will ensure that this field is initialized in every path of execution. See here to learn more about the blank final variable in Java.


6) Can you change the state of the object to which a final reference variable is pointing?
Yes, you can change the state of the object referred by a final variable. This is one of the tricky concept in Java and often cause subtle errors. One of the most common examples of this is Collection classes e.g. ArrayList or HashMap referenced by a final variable. You can still add, remove and update elements but you cannot change the final variable to point to another collection. This is different then immutable or read-only collection in Java, where you cannot perform any add, remove or update operation once created.


7) Can we make an array final in Java? Can you change its elements?
Yes, you can make an array final in Java and you can change it's elements as well. This is actually the follow-up to the previous question, both array and collection classes can be made final and you can still change their elements.


8) Can you make a Collection final in Java e.g. ArrayList? What is the impact?
Yes, you can make a Collection final in Java. The impact is nothing but the final variable cannot be swapped with another Collection, but you can still add, remove and update elements in ArrayList or any collection classes. This is again related to previous two questions. See Big Java: Early Objects to learn more about this concept.


9) What is the difference between abstract method and final method in Java?
The abstract method is incomplete while the final method is regarded as complete. The only way to use an abstract method is by overriding it, but you cannot override a final method in Java. You should also remember that a method cannot be both abstract and final in Java because both are opposite to each other.


10) What is the use of final class in Java?
You make a class final when you think it's complete and nobody should alter the feature by creating a subclass. Generally, security sensitive classes are made final in Java e.g. String. Another reason is performance, compiler, and JIT both can make a lot of assumption if a class is final because they know overriding or polymorphism will not come into the picture.

Top 21 Java Final Modifier (Keyword) Interview Questions and Answers


11) Can you overload a final method in Java?
Yes, you can overload a final method in Java, remember overloading is different than overriding and you only need to declare the method with the same name in the same class but different method signature for overloading. See rules of method overloading to learn more about overloading in Java.


12) Can you override a final method in Java?
No, you cannot override a final in Java. You make a method final in Java to prevent overriding, so no question of further allowing overriding.


13) Can we make a static method final in Java?
Yes, you can make a static method final in Java, nothing prevents you making a static method from being a final one as well. In fact, they both go hand in hand.


14) Can we make an abstract method final in Java?
No, you cannot make an abstract method final in Java because, in order to use an abstract method, you must override it but the final method cannot be overridden in Java. The compiler will flag an error if you use both abstract and final keyword together with class or method in Java. See Core Java Volume 1 - Fundamentals to learn more about rules of Java programming.


15) Can we use non-final local variables inside an Anonymous class?
Yes and No, it's not allowed until Java 7, from Java 8 onward you can use a non-final local variable inside an anonymous or local inner class, provided it's effectively final, which means it's not changed after initialized. See here to learn more about the effectively final concept in Java 8.


16) Can you declare Constructor as final in Java?
No, Constructors cannot be made final in Java. The compiler will throw an error if you try to make a constructor final in Java. See here to learn more about constructor in Java.


17) What is constant in Java?
A static final variable is known as constant in Java. They are also known as a compile time constant because of their value at the time of compilation. They are also inlined at the client end, means if you are using a static final variable then its value will be copied to your class at compile time. Which also means that you need to recompile all the classes which use the static final variable, whenever you change the value of a static final field. This has the potential to create subtle bugs. Read Java Coding Guidelines to avoid such mistakes.


18) Are static final variables are thread-safe?
There are two types of static final variables, primitive, and reference. Initialization of all static final variables is thread safe because it's done in static initializer block. Similarly primitive static final variable is also thread-safe because you cannot modify their value once created, but reference static final variable may or may not be thread-safe. If the object to which your final variable is referring is Immutable or thread-safe then it is otherwise not.


19) Can a class be abstract and final at the same time?
No, it's not possible because the only way to use abstract class is by extending it and creating a concrete subclass, while it's not possible to extend a final class in Java.


20) When to make a method final in Java?
You make a method final when you know that it's complete and you want to ensure that it should not be overridden. One of the examples of a final method is template methods from the Template design pattern, which outlines the algorithm. That method should be final so that sub-classes cannot change the algorithm, they are only allowed to customize individual steps which are represented by the abstract method.


21. When to make a class final in Java?
You make a class final when you don't want anyone should extend it. This is mainly done due to security reasons because it also hampers the extensibility of your program. A couple of examples of final classes in JDK is String, Integer, and other wrapper class. See here to learn more about why the String class is made final in Java.


That's all in this list of Java Interview questions based upon the final keyword. We have covered the final variable, methods, and final classes. We also have a couple of questions on the relationship of the final modifier with Java concurrency.

Further Reading
Java Fundamentals, Part 1 and 2
Java Interview Courses for Beginners
Cracking the coding interview - 189 questions and solutions


Other Java Interview Questions topics you may like to explore
  • Top 10 Java Generics Interview Questions and Answers (see here)
  • Top 10 Garbage collection interview questions in Java (see here)
  • Top 50 multithreading and concurrency interview questions in Java (see here)
  • Top 25 collections interview questions in Java (see here)
  • Top 21 Inheritance interview questions in Java (see here)
  • Top 19 method overloading and overriding interview questions in Java (see here)
  • Top 30 OOPS concept interview questions in Java (see here)
  • Top 20 Design pattern and software design interview questions in Java (see here)
  • Top 10 Java String Interview Questions and answers (see here)


4 comments :

Anonymous said...

Is number 4 true?

Unknown said...

You CAN make a local variable final. In fact, it earlier versions of Java it was necessary if you wanted to access that variable from an anonymous class. Now it just has to be effectively final, basically meaning that you treat it as final and adding the final modifier wouldn't cause a compile error.

javin paul said...

@Anonymous, no number 4 is not true, as Sergey suggested you can make a local variable final in Java and it was necessary for a variable to be accessed inside anonymous inner class until Java 8. It's just brain fade, I was probably thinking about static variable. Anyway, I'll correct it. Thanks for pointing it out.

Unknown said...

hi Paul, I am David Mayer from www.java8certificationquestions.com
Nice article, the most curios question is the 13th, Can we make a static method final in Java?
You answered that hey both go hand in hand but I would not say that. I would say that Static methods cannot be overridden but they can be hidden. If you define a static method as final you are basically blocking overhiding from happening!
Going to share this on my socials.
Thanks
David

Post a Comment