Skip to content

Q 31 - Final Keyword #37

Discussion options

You must be logged in to vote

🔒 Purpose of final in Java

The final keyword prevents further modification. It’s Java’s way of saying “this must stay as it is.” Here's how it's used across different contexts:

1. Final Variables

  • Once assigned, cannot be changed.
  • Acts as a constant—typically used for fixed values (like PI or configuration values).
final int MAX_USERS = 100; // Can't be reassigned later

2. Final Methods

  • A method declared final cannot be overridden by subclasses.
  • Useful when you want to lock in behavior and prevent subclasses from altering it.
class Bank {
    final void calculateInterest() {
        // Fixed calculation logic
    }
}

3. Final Classes

  • A class marked final cannot be subclassed.
  • Prevents…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by AmjustGettingStarted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants