• 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

When implementing an interface, is it a good practice to convert all fields into private?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As my topic indicates, when implementing an interface, is it a good practice to convert all fields into private, or keep it as it is?

For example,



Should we do this


Or do this:


Thanks,
-snow
 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The fields in interfaces are really constants so this will not compile/work:


It is good practice to make fields private (called encapsulation) in implementation classes.


 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

If you search for “Java static import”, you will find all sorts of things. This was at the top when I tried it. If you go through that document, you find that some people think it is bad to have too many constants in interfaces at all.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As K. Tsang said, field declarations in interfaces are really constants.

You are not supposed to declare the fields in the interface, just methods (unless you indeed want to declare a constant). Your interface should have looked like this:

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't have anything private in an interface. If you put in variables, they must have an assigned value. If you don't also put "public static final" on them, that is what they will default to.

To add a twist, take a look at this to also find out about default implementations of interface methods.

Les
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

maddy snow wrote:As my topic indicates, when implementing an interface, is it a good practice to convert all fields into private, or keep it as it is?


Just to clarify: When the others say that interface fields are constants, what they mean is that your interface is equivalent to:So whatever you do in your implementing class, you should NOT call its field "id". You'll just cause confusion.

However, as a general rule, you should ALWAYS define instance fields as private. At least until you're much more experienced.

Winston
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic