What is the purpose of the ‘final’ keyword in Java?

Java MCQ: What is the purpose of the ‘final’ keyword in Java?

a) To prevent inheritance of a class
b) To prevent method overriding
c) To declare constants
d) All of the above

Answer:

d) All of the above

Explanation:

The final keyword in Java serves multiple purposes. It can be used to prevent inheritance of a class, to prevent a method from being overridden, and to declare constants.

When a class is declared as final, it cannot be extended by any other class. When a method is declared as final, it cannot be overridden by subclasses. When a variable is declared as final, its value cannot be changed once it has been assigned.

Using the final keyword appropriately helps to enhance security and maintain the integrity of your code by ensuring that certain behaviors cannot be altered.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top