11

What are the pros and cons of extending a JFrame rather than create a new JFrame?

For example:

public class Test extends JFrame {

setVisible(true);

}

or

public class Test {

JFrame test = new JFrame():

test.setVisible(true);

}
Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
Anonymous181
  • 1,863
  • 6
  • 24
  • 27

4 Answers4

11

You should not extend a class, unless you want to actually extend its functionality, in the example you've shown, you should use option 2, as option 1 is an abuse of the extending feature.

In other words - as long as the answer to the question is Test a JFrame? is NO, it should not extend JFrame.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • If I add some methods into the extended class, am I not extending the JFrame? Please, reply to that. Why is the option 1 an abuse of the extending feature? He was not overriding a method called setVisible. Does it mean that I cannot call inherited methods? Why not to finalize the extended JFrame if worried about potential subclasses? I don't think it is **wrong** to extend JFrame to create a window,if it IS A window. – Daniel Katz Aug 23 '13 at 09:13
  • I'm trying to get a better understanding. A school example: A Student extends a Person. Student has the option to log in, Person does not. A Window "NewTask" provides a funcion to create a new Task according to a form, JFrame does not. – Daniel Katz Aug 23 '13 at 09:35
  • 1
    @DanielKatz - When you extend the functionality of a class, or when your new class is a more specific definition of the extended class (Person->Student or TaskFrame->Frame), this is fine, this is the purpose of class extension. Otherwise, you mix two different entities in one class, and you make things more complex and less clear and this harms the basic principles of OOP. Of course, when you have only 3 lines in your class, it doesn't matter much, but this habit will bite you once you write some real application and need to maintain it, so I think the "pain" of writing another class – MByD Aug 23 '13 at 12:33
  • will be worthwhile in the long run. – MByD Aug 23 '13 at 12:33
10

pros of not extending JFrame (or any Swing component for that matter):

  • Avoid unintentional method overrides. I've stepped into this several times, first when I gave my class int getX() and int getY() methods. Try it and you'll see some not so funny abnormal behaviors.
  • Simplify the method options available when using Eclipse or NetBeans to only those methods you've created. This is actually my favorite advantage.
  • Gearing your GUI's to create JPanels rather than JFrames which increases deployment flexibility 100-fold.
  • And most importantly, exposing only that which needs exposing.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 3
    *"Gearing your GUI's to create JPanels rather than JFrames.."* Sure, but be careful not to fall into the same trap of extending the panel (unless implementing custom paint behavior etc.). – Andrew Thompson May 04 '12 at 05:43
2

If extending JFrame, I would want to modify/customize my current Jframe class and so subclass can use this customized implementation.

If there nothing that I want to do change in the JFrame class, just use the existing like in second snippet that you've given in your code. By use, I mean by creating other JComponent (button/label for examples), etc, in a JPanel and create an object Test and set the JFrame contentPane to this object. Something like

public class Test extends JPanel {

   public class() {
    // add buttons/label here
   }
   ...

   private static void createAndShowGUI() {
       JFrame frame = new JFrame();

       Test object = new Test();
       frame.setContentPane(object.setOpaque(true));

       frame.setVisible(true);
   }
...
}
Jasonw
  • 5,054
  • 7
  • 43
  • 48
0

One of the rules of OOD: If you can not inherit (extend) the classes you do not inherit their.

Inheritance increases the complexity and connectedness of the program and potentially lead to new errors. In your case there are not any reasons to extend class.

You should extends classes only when you need to get access to protected members and/or or get polymorphic behavior (override virtual methods).

Aligus
  • 1,585
  • 1
  • 9
  • 11