What is called the replaceable method in the constructor?

Asked

Viewed 1,754 times

6

In a class that extends JFrame, I have some calls on the builder, as can be seen below:

 public ListaDeOficiosUI() {
        try {
            this.oficioController = new OficioController();
            this.initComponents();
            //o alerta é exibido nas 3 chamadas de métodos
           //  seguintes, todos da classe JFrame
            this.setTitle(GerOficiosUI.TITULO + " - Lista de Oficios");
            this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            this.addWindowListener(
                    new WindowAdapter() {
                        @Override
                        public void windowClosed(WindowEvent e) {
                            GerOficiosUI x = new GerOficiosUI();
                            x.setLocationRelativeTo(null);
                            x.setVisible(true);
                        }

                        @Override
                        public void windowOpened(WindowEvent e) {
                            requestFocus();
                        }
                    });
            this.setRowSorter();
        } catch (ExcecaoErroIO ex) {
            PrintMessageUI.exibirError(this.getInstance(), ex.getMessage());
            System.exit(0);
        }
    }

However, netbeans is highlighting some passages with the message Call for replaceable method in the constructor, as can be seen in the print below:

imagem da mensagem de alerta do netbeans

Code does not generate errors, normally runs without any kind of problem.

What does this warning message mean? Ignoring it may bring some problem in the application?

  • The function he claims is a virtual method?

  • @Rodrigoguiotti do not know this concept of virtual method in java, I can’t tell you that :/

  • If the method is not static, private or final, it can be replaced by another through inheritance and the class it inherits from Listdeoficiosui could change the behavior of its constructor.

  • @Rodrigoguiotti no class inherits ListaDeOficiosUI, it is she who inherits JFrame, as it is a canvas. But as a daughter class can override the constructor of the upper class?

3 answers

6


What is

Replaceable method means a method that can be superscripted by a subclass.

The warning

This is just one Warning of its IDE on a potential problem, not necessarily an error or something inherent to Java.

The problem in this case is that your constructor is depending on a method that can be overridden by a subclass. If this occurs, the subclass can change the behavior of the method and make the construction of the class unpredictable.

This breaks the inheritance contract, because if the subclass wants to change the boot behavior, it must override the constructor and not a method that affects the superclass constructor.

Personally, I don’t consider this a very serious case. In most cases, as this should be, this is simply not a class that will be overwritten and that will never happen. Then I would simply turn off this warning in the IDE settings.

Some Ides allow you to omit the warning in the code snippet, in the class, in the project or globally.

The solution

Surely it’s not putting the call into another method private as the other reply says, as you are simply adding unnecessary code in order to omit a warning from the IDE, but that does not solve in anything the possible problem pointed, that is, to allow the superscription of a method used in the constructor.

If this class is not designed to be inherited, the solution is simply to do nothing and disable the warning.

If you are designing the class for inheritance, a possible solution would simply make the method setTitle private. It makes no sense for a subclass to change something that was set in the superclass builder.

If the subclass needs to change the title, add a new constructor with a parameter String titulo. Example:

public ListaDeOficiosUI() {
    this("Título default");
}
public ListaDeOficiosUI(String titulo) {
    ...
    this.setTitulo(titulo);
    ...
}
private setTitulo() { ... } 

This way you maintain the expected behavior of the superclass and the child classes can explicitly change the title, making clear the behavior contract in the hierarchy.

Note on the JFrame

How you are using the inherited method of JFrame, the visibility of the setTitulo and other methods.

In this specific case, you could use the JFrame that already receives the title. Example

public ListaDeOficiosUI() {
    super("Meu Título");
}

For the other cases is not very good.

However, another approach is not to use inheritance. You nay need to extend JFrame! You can simply reference an attribute in your class that represents the screen, like this:

public class ListaDeOficiosUI {
    private JFrame frame;
    public ListaDeOficiosUI() {
        frame = new JFrame();
        frame.setTitle("Título");
        ....
    }
    ...
}

I consider this approach better because it avoids inheritance where it is simply not needed.

Considerations

Tip: avoid using Ides in English. I don’t mean any harm from those who translated, but translating technical terms are hardly reliable.

  • As a matter of fact, my class is already inheriting JFrameis a screen) and the methods that give this Warning are called from the class I’m already inheriting. I think I’ll leave it as it was, I don’t intend to reuse(or.

  • @Okay, I forgot that you were probably inheriting from JFrame. In this case there is no problem anyway, but you can also replace the title case by a call to the superclass constructor, because Jframe has a constructor that receives the title. Example: super("Meu Título"); in the first line of the constructor.

0

The way to fix this problem is not to use methods that are replaceable (since Java does not use the virtual word) within the constructor.

Only use static, private or final methods in manufacturers.

  • But the methods that give the alert are not of my class, they are of the class that mine inherited, as I will modify the signature of something inherited?

  • You can change the Jframe class?

  • Actually, she’s from the swing API, see. I’m just reusing it.

  • Oh yes. So you just take out of the constructor the methods you are using that can be modified. From what I saw in your print, setTitle, setDefaultCloseOperation and addWindowListener.

  • It would force me to have to devise methods to overwrite almost every method in the parent class, the code would get messy. Instead of calling in the constructor, if I play these replaceable calls in another private method and call this private method in the constructor, circumvent the ne message?

  • That, for that reason I edited the answer, to make it clearer.

  • Now that I’ve noticed your comment in the reply about that very thing, thanks! :)

Show 2 more comments

-1

In Netbeans, with the project window open, I solved the problem by clicking:

Navegador -> JFrame -> Eventos -> componenteResized. 

In the code :

setExtendedState(Frame.MAXIMIZED_BOTH);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.