Cannot extend from a final class

I am working on a project in Java and I am trying to create a new class that will extend from an existing final class. However, when I try to extend the class, I get an error message saying “Cannot extend from a final class.” This is causing me a problem because I need to add some additional functionality to the existing class, but I am unable to do so because it is marked as final.

Here is an example of the code I am trying to use:

final class MyFinalClass {
    // existing class code
}

class MyNewClass extends MyFinalClass {
    // new class code
}

The error message that I’m getting is:

MyNewClass.java:3: error: cannot inherit from final MyFinalClass
class MyNewClass extends MyFinalClass {

I’m stuck and don’t know what to do, I need to extend the functionality of the existing class but I can’t do it because it is final.

1 Like

I mean, the obvious solution would be simply to not make the class final, is it absolutely necessary to make this class final? You cannot extend a final class, which is kind of the point of the final keyword.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.