aspose file tools*
The moose likes Beginning Java and the fly likes Implements Vs Extends on a constants file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Implements Vs Extends on a constants file" Watch "Implements Vs Extends on a constants file" New topic
Author

Implements Vs Extends on a constants file

Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
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

Joined: Oct 13, 2005
Posts: 39436
    
  28
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
Marshal

Joined: Mar 22, 2005
Posts: 42292
    
  64
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.


Ping & DNS - my free Android networking tools app
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
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

Joined: Oct 27, 2005
Posts: 19723
    
  20

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Implements Vs Extends on a constants file