final Methods
Methods that cannot be overridden
4 min read
final Methods in Java
A final method cannot be overridden by subclasses. Use it to prevent behavior modification in inheritance.
Why Use final Methods?
- Prevent Override: Lock down critical behavior
- Security: Subclasses can't alter sensitive logic
- Design Intent: Signal that method shouldn't change
- Performance: JVM can inline final methods (minor)
🔑 Note: Private methods are implicitly final (can't be overridden anyway). Static methods can't be overridden either (they're hidden).
💡 Related: final class = cannot be extended. final variable = cannot be reassigned.
Code Examples
Basic final method usage for security
java
1public class BankAccount {
2 private double balance;
3
4 // final method - cannot be overridden
5 public final double getBalance() {
6 return balance;
7 }
8
9 // final method - security critical
10 public final void verifyPassword(String password) {
11 // Critical logic that shouldn't be modified
12 if (!checkPassword(password)) {
13 throw new SecurityException("Invalid password");
14 }
15 }
16
17 // Non-final - can be overridden
18 public void displayInfo() {
19 System.out.println("Balance: " + balance);
20 }
21}
22
23public class SavingsAccount extends BankAccount {
24 // ERROR: Cannot override final method!
25 // public double getBalance() { return 0; }
26
27 // OK: Can override non-final method
28 @Override
29 public void displayInfo() {
30 System.out.println("Savings Account");
31 super.displayInfo();
32 }
33}Template method pattern with final
java
1// Template Method Pattern - common use of final
2public abstract class DataProcessor {
3
4 // Template method - final to preserve algorithm structure
5 public final void process() {
6 readData(); // Step 1
7 validateData(); // Step 2
8 transformData(); // Step 3
9 saveData(); // Step 4
10 cleanup(); // Step 5
11 }
12
13 // Abstract methods - subclasses implement
14 protected abstract void readData();
15 protected abstract void transformData();
16 protected abstract void saveData();
17
18 // Default implementations - can override
19 protected void validateData() {
20 System.out.println("Default validation");
21 }
22
23 // Final helper - cannot override
24 protected final void cleanup() {
25 System.out.println("Cleaning up resources...");
26 }
27}
28
29public class CSVProcessor extends DataProcessor {
30 @Override
31 protected void readData() {
32 System.out.println("Reading CSV file");
33 }
34
35 // Cannot override process() or cleanup() - they're final!
36}Final with different access modifiers
java
1// Final methods in Java core classes
2// String class is final - cannot be extended
3// Many methods in Object class scenarios:
4
5public class Parent {
6 // final prevents override
7 public final void criticalOperation() {
8 System.out.println("Critical logic");
9 }
10}
11
12// Combining final with other modifiers
13public class Example {
14 // public final - accessible, not overridable
15 public final void publicFinal() {}
16
17 // protected final - inherited, not overridable
18 protected final void protectedFinal() {}
19
20 // static methods can't be overridden anyway
21 // but can be "hidden" by subclass
22 public static void staticMethod() {}
23
24 // private is implicitly final
25 private void privateMethod() {}
26}
27
28// final class - ALL methods are effectively final
29public final class ImmutableClass {
30 // No subclass can exist, so no overriding possible
31 public void anyMethod() {}
32}Real-world final methods and comparisons
java
1// Real-world examples
2public class HttpClient {
3 // Final to ensure consistent connection handling
4 public final void connect(String url) {
5 validateUrl(url);
6 establishConnection(url);
7 logConnection(url);
8 }
9
10 // Subclasses can customize these
11 protected void validateUrl(String url) {}
12 protected void establishConnection(String url) {}
13 protected void logConnection(String url) {}
14}
15
16// Comparison: final vs private vs static
17public class Comparison {
18 // final - visible to subclass, can't override
19 public final void finalMethod() {}
20
21 // private - not visible to subclass at all
22 private void privateMethod() {}
23
24 // static - belongs to class, "hidden" not overridden
25 public static void staticMethod() {}
26}
27
28class Child extends Comparison {
29 // ERROR: public final void finalMethod() {}
30
31 // OK: This is a NEW method, not override
32 private void privateMethod() {}
33
34 // OK: This HIDES parent static (not override)
35 public static void staticMethod() {}
36}Use Cases
- Security-critical methods
- Template method pattern algorithms
- Methods called from constructors
- Preventing unexpected behavior changes
- API stability guarantees
- Performance optimization (JVM inlining)
Common Mistakes to Avoid
- Overusing final (reduces flexibility)
- Confusing final method with final class
- Thinking private needs final keyword
- Not understanding static method hiding
- Using final on interface default methods
- Breaking polymorphism unnecessarily