What is the impact of declaring a method as final on inheritance?

Declaring a method as final in Java prevents subclasses from providing their own implementation of that method. This ensures that the original behavior defined in the parent class remains unchanged throughout the inheritance hierarchy. It is commonly used when a method contains critical business logic, security checks, or functionality that should not be modified.

Key Points: • A final method can be inherited by child classes but cannot be overridden. • It helps maintain consistent behavior and protects important logic from accidental modification. • Final methods can still be overloaded because overloading depends on method signatures, not inheritance.

Example: Consider a banking application where a method validates transaction security rules. Marking this method as final ensures that no subclass can bypass or alter the validation process, preserving application security.

Code Example:

class Account {

    public final void validateTransaction() {
        System.out.println("Performing security validation...");
    }
}

class SavingsAccount extends Account {

    // Compilation Error
    // Cannot override the final method

    /*
    public void validateTransaction() {
        System.out.println("Custom validation");
    }
    */
}

public class Main {
    public static void main(String[] args) {
        SavingsAccount account = new SavingsAccount();
        account.validateTransaction();
    }
}

Interview Tip: A concise interview answer is: A final method can be inherited but cannot be overridden by subclasses. It is used to preserve the original implementation, enforce consistent behavior across the inheritance hierarchy, and protect critical business logic from modification.