What is the keyword final used for in Java ?
2025
What is the keyword final used for in Java ?
- A.
To make a method unmodifiable
- B.
To declare a variable constant
- C.
To prevent method overriding
- D.
All of the above
Attempted by 50 students.
Show answer & explanation
Correct answer: D
The correct answer is Option D (All of the above). In Java, the 'final' keyword serves multiple purposes depending on the context. When applied to a method, it prevents that method from being overridden by subclasses, effectively making the implementation unmodifiable in derived classes. When used with a variable, it declares that variable as a constant, meaning its value cannot be changed after initialization. Additionally, when applied to a class, it prevents the class from being subclassed entirely. Since options A, B, and C each describe a valid use case of the 'final' keyword in Java—making methods unmodifiable, declaring variables constant, and preventing method overriding respectively—the most comprehensive answer is 'All of the above'. This demonstrates that 'final' is a versatile modifier used to enforce immutability and restrict inheritance across different program elements.