This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
Joined: Apr 26, 2007
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.
P.S: Yes, I use Java 1.5_11 for my project here....
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).