Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Implements Vs Extends on a constants file

 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There fellow Ranchers..,

This question might come in as too amateurish to most of you seasoned ranchers, but I pray you help me find the truth behind
this convention/process. Right from day one in my java career as a fresh graduate engineer, I have been seeing that whenever any
module in my project uses constants, they are all put together in a constants interface, and then every class in need of those constants, would
just implement that interface to use them. But would it hurt to rather put the constants together in a concrete/abstract class, and then the
other classes needing these constants just extend this constant class ? I understand that a java class can extend only one other class, whereas it may
implement any number of interfaces, but other than that, is there any performance bottlenecks/ issues due to the latter approach?
Any help on this "simple" query, is highly appreciated....

Dawn.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you still using Java1.4? If you are on Java5 or Java6, you might be better off putting the constants into a concrete class, as public static final fields, then using static import.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, both of these are "anti-patterns" that should not be used. Using the "extends" approach is a perversion of using subclassing, and while the "implements" approach can be seen with some regularity, it too is to be avoided.

There's much to be said for being able to distinguish a local variable from one defined elsewhere at one glance. If the effort of typing the classname really is too much (and modern IDE's auto-completion feature helps with that), consider using static imports instead.
 
Dawn Charangat
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot....guys, for that quick response...

Of course, I've read about static imports before, but never really had a chance to use them in my programs...
may be this is one place I can start with.... once again, thanks for your suggestions.

Dawn.

P.S: Yes, I use Java 1.5_11 for my project here....
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constants class should have a private constructor:

java.sql.Types is a good example of this approach.

By making the only constructor private, you prevent both initialization (no class can call "new Constants()") and also extending (because the sub classes won't be able to find a superclass constructor to call).
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You also need a package name in your Constants class, or you can't import it.
And if you have only a private constructor, call it "public final class Constants."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic