Top 10 Java Interview Questions On Final Keyword
Top 10 Java Interview Questions On Final Keyword
keyword
Abstract methods must be overridden in sub class where as final methods can not be
overridden in sub class
3. 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.
4. What will happen if we try to extend final class in java?
1. package com.finalkeywordintweviewprograms;
2. public final Class SuperDemo{
3. int a,b;
4.
5. public void show() {
6.
7. System.out.println(a);
8. System.out.println(b);
9.
10. }
11. }
1. package com.finalkeywordintweviewprograms;
2. public Class Sample extends SuperDemo{ //The type Sample cannot subclass the
final class
3. SuperDemo
4.
5. }
No. Its not possible to declare a final variable without initial value assigned.
While declaring itself we need to initialize some value and that value can not be change at
any time.
1. package com.finalkeywordintweviewprograms;
2. public final Class Sample{
3.
4. final int x=12,y=13;
5.
6. public void Method() {
7.
8. x=25;// compile time error:The final field Super.x cannot be assigned
9. y=33;// compile time error: The final field Super.y cannot be assigned
10.
11. }
12.
13. }
Compile time error will come :Cannot override the final method from Super class
9.Can we create object for final class?