Results 1 to 5 of 5
Thread: to Extend or not to Extend
- 12-20-2011, 11:06 PM #1
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
to Extend or not to Extend
Hi,
I have a small code as in below:
My question is on extending:Java Code:package myPackage; import javax.swing.JFrame; public class SimpleInheritance { public static void main (String args[]) { JFrame myFrame = new JFrame(); myFrame.setSize(123,125); myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); myFrame.setVisible(true); }// end main }// end class SimpleInheritance
If my class SimpleInheritance extends JFrame:
1) I will not need to use JFrame.EXIT_ON_CLOSE, but using only EXIT_ON_CLOSE will be enough since I will have access to the variable EXIT_ON_CLOSE.
2) I will be allowed to override methods that JFrame has.
Besides these two options, what else is extending providing me ?
Thank you.
-
Re: to Extend or not to Extend
There are dangers of extending a class when it is not necessary (when you are not altering the innate behavior of the class). I have run into this myself when subclassing JPanel and then giving this class two int fields, x and y with the expected setters and getters. Imagine my surprise when I could never draw the JPanel in the correct location because my unnecessary subclassing resulted in accidentally overriding JComponents getX() and getY() methods.
My advice -- don't subclass another class unless you have a very specific and important reason for doing so. Being able to access constants with less typing is not a good reason in my opinion.
- 12-21-2011, 04:14 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: to Extend or not to Extend
In almost every case of an extended JFrame (and often with a JPanel) it is not a genuine extension, by which you are not adding functionality.
You are simply extending it to stick the code that fills the JFrame somewhere.
It's a bit like extending an ArrayList simply to write:
Java Code:public MyArrayList extends ArrayList { public MyArrayList() { add(some object); add(some other object); etc etc. } }
- 12-21-2011, 04:19 PM #4
Senior Member
- Join Date
- Nov 2011
- Location
- Turkey
- Posts
- 378
- Blog Entries
- 24
- Rep Power
- 2
Re: to Extend or not to Extend
Thank you..
So extending a JFrame is really not useful ? Or meaningful ?
- 12-21-2011, 04:28 PM #5
Re: to Extend or not to Extend
It is useful and meaningful, if you're actually changing some behavior, such as overriding paintComponent (in which case, extend a JPanel and put it in a JFrame). But if you're only doing it to get around typing JFrame.EXIT_ON_CLOSE, then don't do it. Even if you did extend JFrame, it's bad practice to refer to static values without using the class name.
But if you really want to get around it and don't mind that it's lazy and a bit terrible, you can use a static import:
Java Code:import static javax.swing.JFrame.EXIT_ON_CLOSE; public Lazy(){ JFrame frame = new JFrame(); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); }How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
How to extend two classes
By man4ish in forum AWT / SwingReplies: 4Last Post: 12-31-2009, 02:31 PM -
Extend Jscroll
By cowboy in forum New To JavaReplies: 10Last Post: 12-13-2009, 08:07 PM -
Trying to extend class
By ribbs2521 in forum New To JavaReplies: 4Last Post: 10-29-2009, 06:28 PM -
extend a button
By Omarero in forum New To JavaReplies: 0Last Post: 03-25-2009, 06:57 PM -
using J-classes or extend them ?
By itaipee in forum AWT / SwingReplies: 1Last Post: 01-01-2009, 07:58 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks