Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend a final class in Java

Tags:

Here is my problem actually I'm facing now. I have a class, let's say Foo and this class defines a method called getBar that returns a Bar instance. The class Bar is defined inside Foo and and is declared public static final. What I want to do is to define a class MyFoo that extends Foo but I also want to extend Bar with MyBar by adding my own functionalities (methods, properties,etc). I also want getBar to return MyBar.

The problem is Bar is final. Here is an illustration of what I want to do :

public class Foo {    Bar bar = new Bar();    public Bar getBar(){       return bar;   }      ....     public static final class Bar {      }  } 

What I want to do is :

public class MyFoo extends Foo {      public MyBar getBar(){ // here I want to return an instance of MyBar     }    public static final class MyBar extends Bar { // That's impossible since Bar is final    }  } 

How can I achieve that?

[EDIT] I am adding more details to my question. I am actually developing a plugin for Jenkins and after searching, I realize there is a plugin that offers the base functionalities that I want to do, Foo and Bar. I want to custom Foo and Bar, so it can fits my needs. Foo is the main class of the plugin and extends a class called Builder. Foo defines a method called perform which contains all the operations for performing a build. Bar contains operations for configurations purposes and Foo needs Bar to get those informations.

So what I want to do is to extend Foo with MyFoo and add some configurations with MyBar. And as you see, MyFoo will need to get those configuations by calling getBar();

The second thing is I have no control of the instanciation of MyFoo and MyBar

What blocks me, is that I cannot extend Bar. How can I achieve that?

like image 888
Dimitri Avatar asked Nov 08 '12 17:11

Dimitri


2 Answers

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

like image 144
Jon Skeet Avatar answered Oct 09 '22 17:10

Jon Skeet


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.

like image 41
flawyte Avatar answered Oct 09 '22 17:10

flawyte