• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interface having constants

 
Ranch Hand
Posts: 203
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic