Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubts regarding AWT/Swing Design

 
sarathy
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have good programming exp. in JAVA. But it seems that i dont get to understand the AWT/Swing design fully. Consider the following 2 cases.

java.lang.Object
extended by java.awt.Component
extended by java.awt.Button

java.lang.Object
extended by java.awt.Component
extended by java.awt.Container
extended by javax.swing.JComponent
extended by javax.swing.AbstractButton
extended by javax.swing.JButton

I believe that a component is a widget such as Button, Label etc ..
How come in AWT that the Component is directly a subclass of Object.

But in Swing it comes well below in the heirarchy. I was wondering if this design was correct.

Also the inheritance logic does'nt seem to work well in the second case.
How can a JButton class ever be a subclass of Container? class. A container is one which contains other components, right?

How can a Container class be a subclass of Component class ?

Please clarify,
Sarathy
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sarathy:
I was wondering if this design was correct.


Well, it's not pretty, but it works. Seems like it was the best that came to the mind of the Sun developers. There might have been more elegant solutions, but I guess they also would have been more complex.

It's a compromise between elegance and simplicity, I think.


How can a JButton class ever be a subclass of Container? class. A container is one which contains other components, right?


You are right, that doesn't make much sense, semantically.

The problem is that the Sun developers wanted to reuse the AWT code for Swing by inheritance (which makes kind of sense). It would have made more sense for JComponent to inherit from Component, and to have a JContainer class that inherits from the AWT Container. But then JContainer also had to inherit from JComponent at the same time (see below), which cannot be done in Java (because Java doesn't allow mutliple implementation inheritance).


How can a Container class be a subclass of Component class ?


It's the Composite design pattern. A Container is a component that is composed of other components. And those other components can be containers again (because containers are components). Very elegant.

Does that help?
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"sarathy", welcome to the ranch. Please take a few moments to read the Javaranch naming policy. After reading the policy, you can change your name here. We try to be friendly around here but we do have our rules, and not following the naming policy can result in having your account deleted.

We'd prefer to keep you with us.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Component and Container should have been interfaces. The problem is that when AWT was originally designed, nobody had much experience programming in Java. The core APIs are full of this sort of thing -- design decisions that were obvious mistakes, but now it's too late to do anything about it.

It's kind of funny if you think about it -- the Java code used by the largest number of Java developers are the APIs themselves, and yet this is some of the least idiomatic Java code in existience, since it was written by people who were brand-new to working in Java!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic