Disallow subclasses from overriding Java method

John picture John · Oct 23, 2012 · Viewed 11.1k times · Source

Suppose I have a method in a Java class, and I don't want any subclass of that class to be able to override that method. Can I do that?

Answer

yotommy picture yotommy · Oct 23, 2012

You can declare the method to be final, as in:

public final String getId() {
   ...
}

For more info, see http://docs.oracle.com/javase/tutorial/java/IandI/final.html