Alex Rivera | Logout

Disallow subclasses from overriding Java method

Asked 2012-10-22T23:30:58.090
38

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?

Edit
Report

1 Answer

65

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

answered 2012-10-22T23:32:19.303

Your Answer