Transcript for:
Understanding the Final Keyword in Java

Hello everyone, today's video we are going to see all the interview questions related to final keyword. This keyword is very important in Java. Basically, final means constant. So, let's start with the first question.

So, first question you can see on the screen, what does the final keyword means? You should be explaining this question in a way where final keyword means constant, it cannot be changed. In Java, final keyword is used for different different purposes.

For example, For variables, you cannot change value of variable. Whereas in case of methods, you cannot override it. Whereas in case of class, you cannot extend it.

And extending means changing feature, adding feature. That's a modification to an existing class. Whenever you are overriding a method, that is also considered as some modification.

So final keyword itself says cannot be changed. So in case of this question, you should be Little more elaborating the answer the way which I told you. So let's move to the next question.

The question is, how do you prevent a class from being inherited? Answer is same, make your class as a final. Next question, how can you prevent a method from being inherited?

In the sense, if you don't want to inherit the method, you can make your class as a final. Nobody can create a subclass. that's why nobody can overwrite this is one answer another answer is you need to make your method as final so that nobody can overwrite that method it's very simple how can you prevent variables from being altered you can prevent variables from altering altering means modifying changing by making that variable as a final so nobody can assign new value to it but i want to explain this in detail if any class is there class a and if it has a variable that is final int a equal to 10. now nobody can assign new value to it but remember you can tell this in more detail to an interviewer you can still do like this class a if this final variable is not initialized then from constructor you can assign some value to it. So this is allowed in Java.

Now this value can also come from anyone. I mean to say while creating object, you can assign different values also. This is little difficult explanation, but if you can understand, you can tell this also to a person who is asking you this question. Coming to next.

Next question is what is pure constant in Java? Pure constant, try to understand this word. Pure constant means final static. One copy storage and nobody will be changing a value.

So that is answer. Next, can you combine abstract, final and public keywords in a class? Yes, you can.

Abstract and final. Try to understand single word. Next question, can you combine abstract, final and public keywords in a class declaration? The answer is no, because abstract means you need to Extend.

Final means you cannot extend. So it's a conflict. So it's not allowed.

You can have any single keyword out of it. That's possible. Is it possible to declare a variable as both final and abstract?

No. Because variable cannot be abstract first of all. So there itself you will get compile time error. But variable can be only final.

Coming to next question. Can you have a static final variable in Java? Answer is yes. These variables are called as pure constant.

In companies In projects, many times constants.java file people will make and there they will be declaring variable as public final static int pi some value. Maybe public final static float acceleration due to gravity equal to 9.8. So, this is a way in which people define pure constants.

Next question. Are there any inbuilt classes in Java that are declared as a final? Yes, all wrapper classes, integer, float, all are final classes, as well as string class is also a final.

Coming to next question, how does the final keyword contribute to the security and stability of Java? Because if any variable is there which is universal constant, just imagine if you are putting some value into it which is not at all possible, like pi, 3.14, it's a mathematical keyword. pi pi 3.14 even if i wish or somebody will wish it should not be changed therefore to secure the value of that variable we need to make it as a final and that will make our project good. So in this video, in this chapter, we have seen all the interview questions related to final. Now in next video, we will be exploring more about more interview questions for more topics.

Thank you.