简体   繁体   中英

Cannot change public variable type in a subclass

I need to make a subclass changing variable type.

My purpuse is to create a class that dynamically loads objects onto a form. This form can be a JFrame or JInternalFrame , so I want a class leading a property form that can be one of JFrame / JInternalFrame , so I can write methods and functions without duplicate code.

Something like

public class form {

    public javax.swing.JFrame frm;
...... methods and functions.

    public void init(String title)
    {

        frm = new JFrame(title);
    }

}

and

class form_children extends form {
    public javax.swing.JInternalFrame frm;

    public void init(String title)
    {

        frm = new JInternalFrame(title);
    }
}

so that when I use them I can do something like this

public form_to_create (String title, Boolean mdi_child)
{
        if (mdi_child)
            frm = new form_children();
        else
            frm = new form();

        frm.init();
        frm.dosomething -----> error for null object
}

but when I use an mdi_child , it gives me error for null object. It seems that frm variable for super class is present, but frm for subclass is not. Why?

I need to make a subclass changing variable type.

My purpose is create a class that dynamically loads objects onto a form. This form can be a JFrame or JInternalFrame, so I want a class leading a property Form that can be one of JFrame / JInternalFrame, so I can write methods and functions without duplicate code.

You are painting yourself in a corner by having your class extend JFrame, JInternalFrame or other top-level (or internal top-level) window, as this forces you to create and display these types of windws, when often more flexibility is called for. In fact, I would venture that most of the Swing GUI code that I've created and that I've seen does not extend JFrame, and in fact it is rare that you'll ever want to do this. More commonly your GUI classes will be geared towards creating JPanels, which can then be placed into JFrames, JInternalFrames or JDialogs, or JTabbedPanes, or swapped via CardLayouts, wherever needed. This will greatly increase the flexibility of your GUI coding.

Thanks.

I want to dynamically create my form, and view that as mdichild window or not child window.

Read an file from disk, analyze it and add panels, controls to my form, so decide if my form has to be a child in a jDesktopPanel as JInternalFrame or simply a JFrame without dependencies.

I thought Java was powerful... but I guess it is not possible.

Your child object has two "frm" properties. The JInternalFrame frm you added in its own definition and the Frame frm it inherited from its father.

Because of polymorphism in form_to_create you are accessing the frm from the superclass, which is not initialized, instead of the JInternalFrame one.

A solution would be to encapsulate the behaviour in methods.

public class Form {
    JFrame frm;

    public void init(String title){
        frm = new JFrame(title);
    }

    public void doSomething(){
        frm.doSomething();
    }
}

public class Form_child{
    JInternalFrame frm;

    public void init(String title){
        frm = new JInternalFrame(title);
    }

    public void doSomething(){
        frm.doSomething();
    }
}

And your form_to_create would look like this now:

public static void form_to_create(String title, Boolean mdi_child){
        Form frm;
        if (mdi_child) {
            frm = new Form_child();
        }else{
            frm = new Form();
        }

        frm.init(title);
        frm.doSomething();
    }

By doing it this way, you are only exposing the behaviour through the doSomething() method, and the rest of the program doesn't care if a JFrame or a JInternalFrame is the one behind.

Even better, make an Interface that defines the common behaviour and have both form classes implement it. That way Form_child doesn't inherit a JFrame property it doesn't use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM