Thomas said:
It is an urban legend that extending JFrame (or other GUI classes) is a
bad idea. If you want a special JFrame-based reusable component it is a
good idea to subclass JFrame, and implement your special component in
accordance with the JavaBeans specification. That way you can add you
new component to the pallet of a bean-aware tool (IDE, GUI builder, etc.).
Bollocks.
Dangerous bollocks at that. Extending classes without need is pure
incompetence.
Always prefer composition over inheritance. Prefer single inheritance of
interface.
JFrames tend to get inherited just for a piece of bad procedural coding.
If you inherit then you are forcing an unnatural organisation onto your
code. It stops the code being used for other components. You are
massively increasing dependencies. You can no longer use JApplets,
JInternalFrames, JDialogs or a JFrame that someone else has extended
from (perhaps even with a good reason). You can't move your code to use,
say, a panel of a tabbed pane. Flexibility is down the drain.
"Reusability" (and it seems like that word is the preserve of the
clueless) is down the drain. Don't specialise your code (but don't
generalise it either).
I've been reading "Killer Games Programming (in Java)". A lardy O'Reilly
book, big on code but light on solid information. It's very difficult to
see what is an API call and what is a call to the authors code. Do you
know all the methods in all the classes that extend Component? I've seen
isValid overridden. Seems eminently sensible. Isn't.
Thread is much the same (only much, much lighter). If you start
subclassing, then try introducing a thread pool...
Also, inheritance breaks encapsulation and is a general menace to society.
You've probably seen a lot of inheritance in a lot of books. I suggest
that those books are bad, and can safely be burnt.
You might like to check out:
Effective Java
-- Josh Bloch
Java Puzzlers: Traps, Pitfalls and Corner Cases
-- Neal Gafter & Josh Bloch
If you can get to see him live, Kevlin Henney is very good. Did a quick
google and found this (I wasn't looking for Java or Threads)
http://jupiter.robustserver.com/pipermail/cpp-threads_decadentplace.org.uk/2004-October/000059.html
Tom Hawtin