Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
621 views
in Technique[技术] by (71.8m points)

static - How to extend a final class in Java

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...