It's not a secret anymore!
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Extend JFrame or encapsulate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Extend JFrame or encapsulate?" Watch "Extend JFrame or encapsulate?" New topic

Extend JFrame or encapsulate?

Michal Charemza
Ranch Hand

Joined: Jul 13, 2004
Posts: 86
Hey all,

I've still not decided whether to extend JFrame or have it as a private member variable (of my MainWindow class... which I may rename, but that's by the by)

Most examples on the web and theads ont here and other formum that I've seen seem to say extending JFrame is the best way.

Arguments for extending JFrame:
  • Other classes will be able to use all the public methods of JFrame, thus making your class more flexible.
  • The class does seem to pass the "IS-A" test for a JFrame. It represents a frame on the screen.

  • Arguments for private encapsulation:
  • Other classes will NOT be able to access the public methods of JFrame, so you do not have to think about what will happen if other classes use them.
  • In either case, I will probably have member variables for the various JButtons and JMenuItems, since I will have "hook" methods to register listeners for them. It seems a bit messy to have them both as member variables, and using .add() to add them to the frame.
  • As far as I see it, OOP principles mean that JFrame should be extended when adding some sort of extra JFrame type behaviour. I'm not sure this is the case here. The JFrame is simply being used to display (via add()) the components on the screen. Is this an argument for the "HAS-A" relationship?

  • Does anyone have any comments on my arguments, or even any more arguments for either side? I've searched, but am unable to find much. Links to FAQs or other threads where I may get help gratefully accepted.

    Andrew Monkhouse
    author and jackaroo
    Marshal Commander

    Joined: Mar 28, 2003
    Posts: 11778

    Hi Michal,

    I think you have to go back to your basic concept of whether your new object will be a new form of JFrame, or is it just using the JFrame (the old "is a" versus "has a" issue).

    Are you creating a new JFrame that you (and others) may want to use time and time again? For example, your version of JFrame will always have a blue frame, will always default to exit on close, etc.? And then you can use this to ensure that all your frames are going to have the same look and feel?

    Or are you using a JFrame to hold your buttons, tables and other widgets, in which case it is highly unlikely that anyone would want to use your layout and widgets.

    Hope this helped.

    Regards, Andrew

    The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
    I agree. Here's the link:
    subject: Extend JFrame or encapsulate?
    It's not a secret anymore!