Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extend in Java

 
Florin Florentin
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello.

I know it is not possible to extend two classes in Java. I want to ask you what trick can i do to extend Observable on a Form(who extends javax.swing.JFrame) ?

thank you
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Florin Florentin wrote:I know it is not possible to extend two classes in Java.

Yep. Java doesn't allow multiple inheritance, and for good reason.

I want to ask you what trick can i do to extend Observable on a Form(who extends javax.swing.JFrame) ?

1) In most situations, you shouldn't subclass JFrame. Instead have your GUI programs create a JPanel, and then when you are ready to view the main app, create the JFrame on the fly and place your JPanel into the JFrame's contentPane. Usually it is unnecessary to subclass JPanel but instead extend your classes through composition rather than inheritance (google these terms for some interesting articles).
2) Also, you could just not use Java's ready-made Observer/Observable classes/interfaces and instead roll your own. They're super easy to make and educational too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic