k_rollo
k_rollo

Reputation: 5472

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

Upvotes: 3

Views: 14896

Answers (8)

KernelKoder
KernelKoder

Reputation: 746

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 {

}

Upvotes: 11

Sarz
Sarz

Reputation: 1976

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,

Upvotes: 0

Anand Kadhi
Anand Kadhi

Reputation: 1888

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.

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172518

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.

Upvotes: 1

Patrick W
Patrick W

Reputation: 1567

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.

Upvotes: 1

Stultuske
Stultuske

Reputation: 9437

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.

Upvotes: 1

Dmitry Ginzburg
Dmitry Ginzburg

Reputation: 7461

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

Upvotes: 1

Brett Walker
Brett Walker

Reputation: 3586

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

Upvotes: 2

Related Questions