是否可以在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:23:24  来源:igfitidea点击:

Is it possible to extend a final class in Java?

javainheritancefinalextends

提问by silver

On possible duplicate:

This thread is not asking howto extend a finalclass. It is asking whya class declared as finalcould possibly extend another class.

关于可能的重复:

这个线程不是在问如何扩展一个final类。它在问 为什么一个声明为final可能扩展另一个类的类。



From thisthread:

这个线程:

A finalclass is simply a class that can't be extended.

一个final类只是一个不能扩展的类

However, I have a helper class which I declared to be finaland extendsanother class:

但是,我有一个我声明的辅助类finalextends另一个类:

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 extenda finalclass when I should not be able to in theory?

为什么我能extend一个final时,我不应该能够在理论课?

(I am using Java 7 if that matters.)

(如果重要的话,我正在使用 Java 7。)

回答by KernelKoder

A Classmarked as finalcan extend another Class, however a final Classcan 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

PDFGeneratorcannot be extended as it is marked final. You are extending PdfPageEventHelperwhich is not marked final.

PDFGenerator不能扩展,因为它被标记为 final。您正在扩展PdfPageEventHelper未标记为 final 的内容。

回答by Dmitry Ginzburg

It seems to me, that PdfPageEventHelperis not final(and this javadoc confirms it), so you can extendit, 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

PdfPageEventHelperclass 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 PDFGeneratorclass 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), PDFGeneratoris decleared as final so the class PDFGeneratorcan't be extended by any class.Try extending PDFGeneratoryou won't be able to extend.

你已经扩展了PdfPageEventHelper(我确定它一定不是最终的),PDFGenerator被取消为最终的,所以这个类PDFGenerator不能被任何类扩展。尝试扩展PDFGenerator你将无法扩展。

回答by Sarz

No and defiantly not, finalkeyword 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的原因:它保证了字段/方法或类的线程安全,