Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
43 views

Top 10 Java Interview Questions On Final Keyword

The document discusses the use of the final keyword in Java and some common interview questions related to it. The final keyword can be used to make a class, method, or variable final. Making a class final means it cannot be subclassed, making a method final means it cannot be overridden, and making a variable final means its value cannot be changed after initialization. Some key points include that abstract methods must be overridden while final methods cannot, final classes cannot be extended, interfaces cannot be declared final, and final variables must be initialized. The most common predefined final class used is String.

Uploaded by

samarnarendra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Top 10 Java Interview Questions On Final Keyword

The document discusses the use of the final keyword in Java and some common interview questions related to it. The final keyword can be used to make a class, method, or variable final. Making a class final means it cannot be subclassed, making a method final means it cannot be overridden, and making a variable final means its value cannot be changed after initialization. Some key points include that abstract methods must be overridden while final methods cannot, final classes cannot be extended, interfaces cannot be declared final, and final variables must be initialized. The most common predefined final class used is String.

Uploaded by

samarnarendra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Top 10 Java interview questions on final

keyword

1. What is the use of final keyword in java?

By using final keyword we can make


Final class
Final method
Final variables
If we declare any class as final we can not extend that class
If we declare any method as final it can not be overridden in sub class
If we declare any variable as final its value unchangeable once assigned.
2. 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
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?

Compile time error will come.

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. }

5.Can we declare interface as final?


No We can not declare interface as final because interface should be implemented by some
class so its not possible to declare interface as final.

6. Is it possible to declare final variables without initialization?

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. }

7. Can we declare constructor as final?

No . Constructors can not be final.

8.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
9.Can we create object for final class?

Yes we can create object for final class.


10.What is the most common predefined final class object you used in your code?

String (for example)

You might also like