This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes javax.swing.SwingConstants why an interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "javax.swing.SwingConstants why an interface?" Watch "javax.swing.SwingConstants why an interface?" New topic

javax.swing.SwingConstants why an interface?

Sharif Muhammad

Joined: Mar 01, 2002
Posts: 2
why are constants usually declared in an interface like in javax.swing.SwingConstants? is there any performance related gains?
Greg Brouelette
Ranch Hand

Joined: Jan 23, 2002
Posts: 144
It may have to do with the fact that multiple inhieritence is not allowed. For example, you may be writing a new Swing component that extends JComponent and you also want to have access to those constants.
So you would extend JComponent and implement SwingConstants. That way you'd have access to all the methods in JComponent as well as all the constants in SwingConstants.

For a good Prime, call:<br />29819592777931214269172453467810429868925511217482600306406141434158089
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
Actually this is a topic that pure Object Oriented programmers LOVE to argue over .
Some say that putting the constants in an interface is quick easy and intuitive.
(ex: Michael Ernest in this post)
Others say that Interfaces should ONLY be used to declare TYPEs and that using it for constants is a dirty way of doing what SHOULD be done through static variables, as is done in the Math class.
(ex: see "Effective Java" - Josha Block, p 89 Item 17 "Use interfaces only to define types")
Some Pros
I agree. Here's the link:
subject: javax.swing.SwingConstants why an interface?
It's not a secret anymore!