File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javax.swing.SwingConstants why an interface?

 
Sharif Muhammad
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
why are constants usually declared in an interface like in javax.swing.SwingConstants? is there any performance related gains?
thanks
 
Greg Brouelette
Ranch Hand
Posts: 144
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic