Is it possible to extend a final class in Java?

做~自己de王妃 提交于 2020-12-12 04:02:13

问题


On possible duplicate:

This thread is not asking how to extend a final class. It is asking why a class declared as final could possibly extend another class.


From this thread:

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

However, I have a helper class which I declared to be final and extends another class:

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.

Why was I able to extend a final class when I should not be able to in theory?

(I am using Java 7 if that matters.)


回答1:


A Class marked as final can extend another Class, however a final Class can not be extended.

Here is an example:

This is allowed

public class Animal {

}

public final class Cat extends Animal {

}

This is not allowed

public final class Animal {

}

public class Cat extends Animal {

}



回答2:


PDFGenerator cannot be extended as it is marked final. You are extending PdfPageEventHelper which is not marked final.




回答3:


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.

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




回答4:


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.

In your example, try creating a new class, that does extend your final class:

public class A extends PDFGenerator{}

and see what happens.




回答5:


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.




回答6:


PdfPageEventHelper class is not made as final and hence you are able to extend that class. The final class cannot be extended.

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.




回答7:


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.




回答8:


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;

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.

It totally depends on requirements which object should/must set as final.

Reason of using final: It ensures the thread safety for field/method or class,



来源:https://stackoverflow.com/questions/30070063/is-it-possible-to-extend-a-final-class-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!