是否可以在 Java 中扩展 final 类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30070063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to extend a final class in Java?
提问by silver
On possible duplicate:
This thread is not asking howto extend a
final
class. It is asking whya class declared asfinal
could possibly extend another class.
关于可能的重复:
这个线程不是在问如何扩展一个
final
类。它在问 为什么一个声明为final
可能扩展另一个类的类。
From thisthread:
从这个线程:
A
final
class is simply a class that can't be extended.
一个
final
类只是一个不能扩展的类。
However, I have a helper class which I declared to be final
and extends
another class:
但是,我有一个我声明的辅助类final
和extends
另一个类:
public final class PDFGenerator extends PdfPageEventHelper {
private static Font font;
private PDFGenerator() {
// prevent instantiation
}
static {
try {
BaseFont baseFont = BaseFont.createFont(
"/Trebuchet MS.ttf",
BaseFont.WINANSI,
BaseFont.EMBEDDED
);
font = new Font(baseFont, 9);
} catch(DocumentException de) {
de.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}
public static ByteArrayOutputStream generatePDF() throws DocumentException {
Document doc = new Document();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter pdfWriter = PdfWriter.getInstance(doc, baosPDF);
try {
// create pdf
} catch(DocumentException de) {
baosPDF.reset();
throw de;
} finally {
if(doc != null) {
doc.close();
}
if(pdfWriter != null) {
pdfWriter.close();
}
}
return baosPDF;
}
}
Eclipse does not detect anything wrong with it. I have tested the class and the PDF was successfully generated without error.
Eclipse 没有检测到它有什么问题。我已经测试了该类并且成功生成了 PDF,没有错误。
Why was I able to extend
a final
class when I should not be able to in theory?
为什么我能extend
一个final
时,我不应该能够在理论课?
(I am using Java 7 if that matters.)
(如果重要的话,我正在使用 Java 7。)
回答by KernelKoder
A Class
marked as final
can extend another Class
, however a final Class
can not be extended.
甲Class
标记为final
可以扩展另一个Class
,然而final Class
不能扩展。
Here is an example:
下面是一个例子:
This is allowed
这是允许的
public class Animal {
}
public final class Cat extends Animal {
}
This is notallowed
这是不容许
public final class Animal {
}
public class Cat extends Animal {
}
回答by Brett Walker
PDFGenerator
cannot be extended as it is marked final. You are extending PdfPageEventHelper
which is not marked final.
PDFGenerator
不能扩展,因为它被标记为 final。您正在扩展PdfPageEventHelper
未标记为 final 的内容。
回答by Dmitry Ginzburg
It seems to me, that PdfPageEventHelper
is not final
(and this javadoc confirms it), so you can extend
it, still you can't extend this new class PDFGenerator
.
在我看来,这PdfPageEventHelper
不是final
(这个 javadoc 证实了这一点),所以你可以extend
,但你仍然不能扩展这个新类PDFGenerator
。
You may try to do it, though:
不过,您可以尝试这样做:
private Object abacaba = new PDFGenerator() {
};
Then you'll get compilation error:
然后你会得到编译错误:
BlahBlah.java:n: error: cannot inherit from final PDFGenerator
BlahBlah.java:n: 错误:无法从最终的 PDFGenerator 继承
回答by Stultuske
Why was I able to extend a final class when I should not be able to in theory?
为什么我在理论上不应该扩展最后一堂课?
You weren't. your final class extended another class, it wasn't extended itself.
你不是。你的最后一堂课扩展了另一堂课,它本身并没有扩展。
final classes (String is an example) are 'protected' against being inherited. You can not extend them.
final 类(String 是一个例子)被“保护”以防止被继承。你不能扩展它们。
In your example, try creating a new class, that does extend your final class:
在您的示例中,尝试创建一个新类,它确实扩展了您的最终类:
public class A extends PDFGenerator{}
and see what happens.
看看会发生什么。
回答by Patrick W
Note that PDFGenerator extends PdfPageEventHelper
. That means that PDFGenerator is final but PdfPageEventHelper is not a final class. If you try to extend the PDFGenerator, then you will get exceptions if not errors.
请注意PDFGenerator extends PdfPageEventHelper
。这意味着 PDFGenerator 是最终的,但 PdfPageEventHelper 不是最终的类。如果您尝试扩展 PDFGenerator,那么您将收到异常(如果不是错误)。
回答by Rahul Tripathi
PdfPageEventHelper
class is not made as final and hence you are able to extend that class. The final class cannot be extended.
PdfPageEventHelper
类不是最终的,因此您可以扩展该类。最后一课不能延长。
If you try to extend PDFGenerator
class which you have made as final then you will see that you are not able to extend that as its marked final.
如果您尝试扩展PDFGenerator
已创建为 final 的类,那么您将看到您无法将其扩展为标记为final。
回答by Anand j. Kadhi
You have extended the PdfPageEventHelper
(It must not be final i'm sure), PDFGenerator
is decleared as final so the class PDFGenerator
can't be extended by any class.Try extending PDFGenerator
you won't be able to extend.
你已经扩展了PdfPageEventHelper
(我确定它一定不是最终的),PDFGenerator
被取消为最终的,所以这个类PDFGenerator
不能被任何类扩展。尝试扩展PDFGenerator
你将无法扩展。
回答by Sarz
No and defiantly not, final
keyword is known in java as is used to restrict the user. It can be applied on variable method and class;
不,绝对不是,final
关键字在 Java 中被称为用于限制用户。可应用于变量方法和类;
Constant Variable:(for all primtive and String) If you make any variable as final, you cannot change the value.
常量变量:(对于所有原始变量和字符串)如果将任何变量设为最终变量,则无法更改该值。
Method:If you make any method as final, you cannot override it.
方法:如果您将任何方法设为最终方法,则无法覆盖它。
Class:If you make any class as final, you cannot extend it.
类:如果您将任何类设为 final,则不能扩展它。
It totally depends on requirements which object should/must set as final.
这完全取决于哪个对象应该/必须设置为 final 的要求。
Reason of using final:It ensures the thread safety for field/method or class,
使用final的原因:它保证了字段/方法或类的线程安全,