Friday, 2 August 2019

Interview qns on final keyword | java

1) Java final variable
If you make any variable as final, you cannot change the value of final variable(It will be constant).

2) Java final method
If you make any method as final, you cannot override it.

3)Java final class
If you make any class as final, you cannot extend it.

Q)Is final method inherited?
Ans) Yes, final method is inherited but you cannot override it. For Example:

Q)Can we initialize blank final variable?
Yes, but only in constructor. For example:

Q)static blank final variable
A static final variable that is not initialized at the time of declaration is known as static blank final variable. It can be initialized only in static block.

Q) What is final parameter?
If you declare any parameter as final, you cannot change the value of it.   int cube(final int n){ n = n+2; }

Q)Can we declare a constructor final?
No, because constructor is never inherited.

No comments:

Post a Comment