This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

interface having constants

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks

Please can you give me a small demo in form a program I want to know that an interface can have constants that are static and final but then how can a class implementing that interface can use those constants while implementing that interface p, please provide a small example..!! Thanks

there is one url is for reference ..webpage
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like any static field, the constants are accessed with the class/interface name.variable name. The general principle being that when you're using the code (if you didn't have the javadoc or source) you can't tell the difference between a constant declared in an interface and a class.

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hi ,

Thanks for the explanation could you please explain once again in detail
(if you didn't have the javadoc or source) you can't tell the difference between a constant declared in an interface and a class.
 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Why do we keep all the constants in an interface ...and why not in abstract class..what so special of keeping the constants in an interface itself...is there any design principle behind it..?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Saral Saxena wrote:Hi ,

Why do we keep all the constants in an interface ...and why not in abstract class..what so special of keeping the constants in an interface itself...is there any design principle behind it..?


Personally, I always thought it seemed like a bad idea, and it led to the awful constants-only-interface antipattern. I don't think there's any design principle behind it. I think it's just for convenience and brevity. I've been working with Java for many years, and I don't think I've ever done it, or ever would.
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation could you please explain once again in detail

(if you didn't have the javadoc or source) you can't tell the difference between a constant declared in an interface and a class.

Just saying that you access the constants in the Car interface the same way you'd access them if Car was a class. Both ways it's still Car.NUM_WHEELS.

I don't do constants in an interface normally either. It seems to me a bad idea to put implementation details such as data into a class that describes a behavior contract. In the example, what if I wanted to make a ThreeWheeledCar? It would still implement the behavior, drive() but the constants would get in the way. At least if you do it in an abstract class you have the option to use the abstract class when doing the implementation, or you could implement the interface directly and create your own constants.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic