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