简体   繁体   中英

How to create an Interface for a class that extends JDialog (or in general another class)

I'm developing a Java application that makes use of Swing. I have a class that extends a JDialog, like this:

public class customDialog extends JDialog {
    //Custom buttons listeners etc.
}

Now what is the manner (best practice) to create an interface for it? I would write:

public interface customDialogInterface {
    //Custom methods signatures
}

and then use the "implement customDialogInterface" in customDialog but in this manner when I use the customDialogInterface in my code I have no access to the JDialog methods. Otherwhise JDialog is a class so I can't extend it in my interface. Furthermore I can't extend customDialogInterface with the interfaces that JDialog implements because one of them is package protected.

What is the correct manner to proceed in those (I suppose commons) cases?


UPDATE 1: I'll try to explain better reformulating my question. There is a programming principle that tells "program using interfaces and not concrete classes". Basing on it, how can I create an interface for my CustomDialog? If I do:

public interface CustomDialogInterface {
    public void doA();
    public void doB();
}

than in the code that uses CustomDialog I have:

CustomDialogInterface myDialog = new CustomDialog();
myDialog.doA();    //OK
myDialog.doB();    //OK
myDialog.setVisible(true);    //ERROR

UPDATE 2: I decided to update again my question because basing on the answer it seems that it is not very clear. It has not many info because it is not yet real code, it is just a theoretical question. Also if I yet found a possible solution I write here again in another form:

  1. In my application I need a JDialog with my info, fields etc. How can I obtain it? Defining a class:

    class MyImplementation extends JDialog { public doA(String txt) { //This is a method specific of my implementation } }

  2. Now, I want to follow the rule of good programming "Program by interface, not by implementation" so I need an interface for my custom dialog

    interface InterfaceForMyDialog { public doA(String txt); }

  3. Obiously the class MyImplementation has to implement the interface so I change the pseudocode above just adding and implements

    class MyImplementation extends JDialog implements InterfaceForMyDialog { public doA(String txt) { //This is a method specific of my implementation } }

  4. Now finally I need to use my new dialog somewhere in the external code (let say somewhere in the main method) like this:

    ... InterfaceForMyDialog myDialogInterface = new MyImplementation(...);

QUESTION: How should you complete the code above to set some text to my custom dialog using the method "doA(String txt)" and then show the dialog itself ? I think it is not possible using this structure and I found a solution (see below) using an abstract class. If I'm wrong or there is a better solution explain please write it here.

You are correct that you can only extend one class; since you are extending JDialog, you can implement 0 or more interfaces, but you cannot extend another class.

If your interface has two methods:

public interface customDialogInterface
{
  public void doA();
  public int  doB();
}

then your class can have

public class customDialog extends JDialog implements customDialogInterface

as long as customDialog has the methods doA() and doB() .

As a note: by convention, both class names and interface names start with capital letters. Java programmers the world over will find it easier to understand your code if you use CustomDialog and CustomDialogInterface .

Ok I found a solution. Instead of declaring CustomDialogInterface as interface I declared it as an abstract class that extends the JDialog. Then I extend this abstract class with the class that provides the implementation. If there is a better solution let me know!

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