Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: 1 [2]
  ignore  |  Print  
  The main idea I didn't get from the Sun tutorial.  (Read 12653 times)
0 Members and 1 Guest are viewing this topic.
Offline ewjordan

Junior Devvie

« Reply #30 - Posted 2008-08-25 19:46:12 »

To me it seems that Riven is saying that the "Inheritance is Evil" rule should apply here.  That rule says that whenever you have a choice, you should prefer composition to inheritance (which means adding functionality by calling out to members rather than extending classes).  That's especially true when you're not actually extending the functionality of a class, you're just using it.

It's a bit tough to justify the rule in a simple example, but suffice it to say that the rule tends to show up pretty high on the OO-advice list of most experienced designers (in any OO language, not just Java - the C++ guys have been screaming about this for decades).  The gist of it is that IS-A is a much tighter coupling between classes than HAS-A, so changes in one class are far more likely to have side effects outside of that file, which makes maintenance that much more of a bitch.  Even Gosling is often quoted (usually by functional programmers that think objects are ruining Real Programming) as saying that the "extends" keyword is one of the worst things about Java - programming to an interface and using composition leads to more maintainable systems in most situations.

In this case it's not a huge deal, since JPanel is for all intents and purposes frozen in time because Sun doesn't want to piss off its entire user base (so you're not likely to have surprise name clashes or anything like that), but it's not gaining you anything to extend it, and it's a bad habit to get into.  The fact that all the Java tutorials use JPanels by extending them is particularly annoying, since it implies that there's some reason you need to extend them in order to use them, which is just not true, and it's very confusing to newcomers.
Offline Riven

« JGO Overlord »

Medals: 1356
Projects: 4
Exp: 16 years

Hand over your head.

« Reply #31 - Posted 2008-08-25 22:13:35 »

Take a look at the average "hello world" introduction to Swing (or worse: introduction to Java!)

How many years does it take one to fully grasp this code:

public class MyFrame extends JFrame implements ActionListener
   public static void main(String[] args)
      new MyFrame("Hello World");

   public MyFrame()
      JButton b = new JButton("click me");
      b.addActionListener(this); // BLACK MAGIC for n00bs

      // what is 'this' ?

      this.setLayout(new BorderLayout());
      this.add(b, BorderLayout.NORTH);
      this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // SAY WHAT?

   public void actionPerformed(ActionEvent event)
      System.out.println("you clicked me");

And why this code crashes or just hangs every once in a while, as we are manipulating Swing from the main thread!

This is very much like how my classmates learned to write their first few lines of Java, when wondering what the difference between an Object, a primitive and a Class was - not to mention Interfaces like ActionListener would be coded 'because the sample code had them'. Anyway... just to show how completely f*cked up most of us (?) got introduced to the wonderful world of programming.

Our teacher was to bad, that he had code like MyFrame, MyButton, MyPanel, MyList - just for implementing the event listeners. And when there was nothing to listen for, I stumbled across:

public class MyCheckbox extends Checkbox {
   public MyCheckbox() {


public class MyMap extends HashMap {
   public MyMap() {

   public void myPut(Object key, Object value) {
     this.put(key, val);

Okay... that's enough offtopic stuff for today.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: 1 [2]
  ignore  |  Print  

EgonOlsen (1569 views)
2018-06-10 19:43:48

EgonOlsen (1541 views)
2018-06-10 19:43:44

EgonOlsen (1141 views)
2018-06-10 19:43:20

DesertCoockie (1568 views)
2018-05-13 18:23:11

nelsongames (1171 views)
2018-04-24 18:15:36

nelsongames (1548 views)
2018-04-24 18:14:32

ivj94 (2306 views)
2018-03-24 14:47:39

ivj94 (1515 views)
2018-03-24 14:46:31

ivj94 (2602 views)
2018-03-24 14:43:53

Solater (879 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!