从重写Java方法中禁止子类 [英] Disallow subclasses from overriding Java method

查看:531
本文介绍了从重写Java方法中禁止子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在Java类中有一个方法,我不希望该类的任何子类能够覆盖该方法。

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?

推荐答案

您可以将方法声明为 final ,如:

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

public final String getId() {
   ...
}

有关详细信息,请参阅http://docs.oracle.com/javase/tutorial/java/IandI/final.html

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

这篇关于从重写Java方法中禁止子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆