You need to design a class that cannot be extended or modified. How would you implement this using the final keyword?

If a class must not be inherited or its behavior altered, the final keyword can be used to restrict extensibility. Declaring a class as final prevents inheritance, while declaring methods as final prevents method overriding. This helps preserve the original implementation, enforce design constraints, and protect critical business logic from unintended modifications.

Key Points: • A final class cannot be extended by any other class. • A final method can be inherited but cannot be overridden in a subclass. • Using final improves design integrity and is commonly used for immutable classes, utility classes, and security-sensitive components.

Example: The String class in Java is declared as final. This prevents developers from extending it and changing its behavior, helping maintain immutability and security across Java applications.

Code Example:

public final class PaymentProcessor {

    public final void processPayment() {

        System.out.println(
                "Processing Payment");
    }
}

class PremiumPaymentProcessor
// extends PaymentProcessor  // Compilation Error
{
}

Interview Tip: A concise interview answer is: To prevent a class from being extended, declare it as final. If only specific behavior must remain unchanged, mark critical methods as final. This ensures the class design remains stable, secure, and protected from unintended modifications.