1

I have two classes: JDialog and JFrame (see below). Now I want them to get some extra functionality, but I want the two classes both to extend my code, without writing the code twice. Is there a way to do that?

java.awt.Window » java.awt.Dialog » javax.swing.JDialog  
java.awt.Window » java.awt.Frame » javax.swing.JFrame

(PS: Actually, I want to "rewrite" the source code of java.awt.Window, adding extra functionality. I know that's not possible, but that would be a "solution", because if I do so, both JDialog and JFrame would extend my brand new methods.)

Example:
Suppose I want to add the method moveWindowToMagicPositionOnScreen(), moving the window to a user-friendly position on the screen—I know, it's just an example. I want the method to apply to both JDialog and JFrame. Using inheritance, I must write two classes, for example MagicJDialog and MagicJFrame, each implementing the method moveWindowToMagicPositionOnScreen() in exactly the same manner. Well, that's pretty redundant. So I don't want to have the code written twice. In some way, I want one class to use the code of the other class.

3
  • 1
    Have you considered composition? Commented Jun 30, 2012 at 4:31
  • PS: You should provide an example of what you seek... Commented Jun 30, 2012 at 4:32
  • Terminology: you're not "rewriting" the source code of java.awt.Window, you're overriding it in your class (assuming there's nothing final in there preventing you from doing that). Commented Jun 30, 2012 at 4:38

4 Answers 4

1

The interface / delegate / composition ideas are probably the "cleanest". But an acceptable and arguably simpler alternative is to make a utility class with a static method.

public static moveWindowToMagicPositionOnScreen(Window window) {
  // calculate new x and y
  window.setLocation(x, y);  // or perhaps setBounds()...
}

In practice, I'd probably put the calculation logic into another method, computeMagicPositionOnScreen(). If it makes sense (it may not) this static utility class could actually be a "real" (non-static) class, say MagicPositionCalculator, that might have multiple instances, say one per each screen, one for really large screens, one for power-users, etc...

Sign up to request clarification or add additional context in comments.

Comments

1

Unfortunately, Java does not support mixins. The easiest and clearest approach to take here would be to use composition, in which case you only have to write the wrappers twice, but the actual implementation lives in a single location.

Comments

1

Like several others have suggested, prefer composition over inheritance. However, you can create an common interface for your method:

public interface Magic {
    void moveWindowToMagicPositionOnScreen();
}

If the code that moves the window is exactly the same create a delegate class:

public class MagicDelegate implements Magic {

    public void moveWindowToMagicPositionOnScreen() {
        // TODO implement logic
    };
}

Then have your two classes implement the Magic interface using MagicDelegate:

public MagicJDialog implements Magic {

    private MagicDelegate magicDelegate;   // TODO instantiate

    public void moveWindowToMagicPositionOnScreen() {
        magicDelegate.moveWindowToMagicPositionOnScreen();
    }
}

and

public MagicJFrame implements Magic {

    private MagicDelegate magicDelegate;   // TODO instantiate

    public void moveWindowToMagicPositionOnScreen() {
        magicDelegate.moveWindowToMagicPositionOnScreen();
    }
}

Side note, the code that moves the window is only partially the same, you can either choose to ignore the MagicDelegate class or create a template class with the common parts:

public abstract MagicTemplate implements Magic {

    public moveWindowToMagicPositionOnScreen() {
        // common logic
        implementationSpecificLogic();
        // more common logic  
    }

    protected abstract void implementationSpecificMethod();
}

Now, the MagicJDialog class and the MagicJFrame class can extend the MagicTemplate class and implement the implementationSpecificLogic() method.

Comments

0

Try this,

I think you want to apply DRY (Don't Repeat Yourself) design principle and partially LSP (Liskov substitution Principle), if you want some method to be stored in a class, and then use this method from other two classes, then I think prefering composing over inheritance will be a good idea. Cause you will be able to use the desired method without inheriting the methods that you don't want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.