How to extend a final class in Java

You can't extend a class that is declared final. However you can create an intermediate class, called Wrapper. This Wrapper will basically contain an instance of the original class and provide alternative methods to modify the state of the object according what you want to do.

Of course this will not give access to the private & protected fields of the final class, but you can use its getters and setters methods, combined with the new methods & fields you declared in the Wrapper class to get the result you want to, or something relatively close.

Another solution is not to declare final a class if there is no valid reason to do it.


You can't, basically. It looks like the developer who designed Bar intended for it not to be extended - so you can't extend it. You'll need to look for a different design - possibly one which doesn't use inheritance so much... assuming you can't change the existing code, of course.

(It's hard to give more concrete advice without knowing more about the real goal here - the design of the overall system.)

Tags:

Java

Static

Final