Final Methods

Final Methods

A school program needs reliable data and clear rules. A final instance method cannot be overridden by a subclass. It can preserve an important report rule while allowing other subclass behavior.

  1. Understand

    A final instance method cannot be overridden by a subclass.

  2. Apply

    Trace the Java example, predict its output and explain the result before running it.

  3. Avoid mistakes

    Final methods can still be called. Preventing overriding is different from preventing overloads with different parameters.

Final Methods

In Java, the keyword final with methods is used to preserve design integrity, prevent misuse, and encourage safe code extension. But what exactly does it mean to make a method final?

What is a final method?

A final method is a method that cannot be overridden by subclasses. This ensures that the core behavior defined in the method remains consistent and unchangeable when inherited.

This is particularly useful when designing frameworks or libraries, where certain base functionalities should never be altered by child classes.

Syntax of final methods

class Parent {
    final void show() {
        System.out.println("This is a final method.");
    }
}

Any class that extends Parent will inherit the method show(), but will not be allowed to override it.

Why use final methods?

  • To prevent unexpected behavior from subclass overrides
  • To ensure critical business logic is not changed
  • To improve performance (some JVMs optimize final methods)

Example: Attempting to Override a Final Method

class Parent {
    final void display() {
        System.out.println("Display from Parent");
    }
}

class Child extends Parent {
    // Uncommenting below will cause a compile-time error
    // void display() {
    //     System.out.println("Display from Child");
    // }
}
Compile-time Error: Cannot override the final method from Parent

Explanation: The compiler blocks the attempt to override the display() method because it’s declared final in the parent class.

Valid Usage: Calling a Final Method in a Subclass

class Parent {
    final void greet() {
        System.out.println("Hello from Parent");
    }
}

class Child extends Parent {
    void welcome() {
        greet(); // Valid: calling inherited final method
    }
}

public class Main {
    public static void main(String[] args) {
        Child c = new Child();
        c.welcome();
    }
}
Hello from Parent

Though the method greet() is final and can’t be overridden, it can still be inherited and called like any other method.

Can a final method be overloaded?

Yes! Overloading is based on the method signature (name and parameters), not inheritance. So you can define methods with the same name but different parameter lists—even if one version is final.

class Example {
    final void test() {
        System.out.println("No parameters");
    }

    void test(int x) {
        System.out.println("One parameter: " + x);
    }
}
No parameters
One parameter: 42

Conclusion

The final keyword in methods is a safeguard. It’s used by developers who want to make sure the rules aren’t rewritten downstream. Use it wisely in scenarios where the base method's logic must be preserved across the inheritance chain.

Key Takeaways

  • final methods cannot be overridden.
  • You can still call and inherit final methods.
  • Final methods can be overloaded.
  • Use them when you want to lock down method behavior for reliability.

Important boundaries and mistakes

Final methods can still be called. Preventing overriding is different from preventing overloads with different parameters.

Try the school example

Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: Final methods can still be called.

Java 17 · Main.java · complete practice program

public class Main  {
  static class FixedReport  {
    final String label()  {
      return "Final report";
    }
  }
  public static void main(String[] args)  {
    System.out.println(new FixedReport().label());
  }
}

Worked answer and reasoning

Java 17 · Main.java · source repeated for this answer

public class Main  {
  static class FixedReport  {
    final String label()  {
      return "Final report";
    }
  }
  public static void main(String[] args)  {
    System.out.println(new FixedReport().label());
  }
}

Expected output

Final report

A final instance method cannot be overridden by a subclass. It can preserve an important report rule while allowing other subclass behavior.

Final methods can still be called. Preventing overriding is different from preventing overloads with different parameters.

Run with javac Main.java, then java Main. The declaration inside Main keeps the helper type available to this standalone example.

Check your understanding

For the complete practice program in this lesson, which output is expected on Java 17?
Choose one answer. You can try again.

Answer and explanation

Final report

A final instance method cannot be overridden by a subclass. It can preserve an important report rule while allowing other subclass behavior. Final methods can still be called. Preventing overriding is different from preventing overloads with different parameters.

What to remember

A final instance method cannot be overridden by a subclass. It can preserve an important report rule while allowing other subclass behavior. Final methods can still be called. Preventing overriding is different from preventing overloads with different parameters.

Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide and Java 17 language specification.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Final Class →