简体   繁体   中英

Is it possible to extend a final class in Java?

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.)

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 {

}

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

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

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.

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.

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 .

您已经扩展了PdfPageEventHelper (它绝不能是最终我敢肯定) PDFGenerator被decleared作为最终使类PDFGenerator无法通过扩展任何class.Try扩展PDFGenerator你将无法延长。

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,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM