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?
You can't extend a class that is declared
final
. However you can create an intermediate class, calledWrapper
. 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 theWrapper
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.If it implements some interfaces, Decorator Design pattern could solve the Problem.
You can't extend a
final
class - that's the purpose of thefinal
keyword.However, there are (at least) two work-arounds:
You cannot extend final classes, that is the point of declaring classes final. You can define an interface that both the final class inherit from and a wrapper class that delegates calls to the wrapped final class. The question is then why to make the class final in first place.
For example we cannot add a StringBuffer in the TreeSet Collection, because TreeSet is not child of the Interface Comparable:
But we can use a wrapper to implement the Comparable:
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.)