{"id":5027,"date":"2014-03-30T18:06:30","date_gmt":"2014-03-30T18:06:30","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/java-final-abstract-class-collection-of-common-programming-errors\/"},"modified":"2014-03-30T18:06:30","modified_gmt":"2014-03-30T18:06:30","slug":"java-final-abstract-class-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/java-final-abstract-class-collection-of-common-programming-errors\/","title":{"rendered":"Java final abstract class-Collection of common programming errors"},"content":{"rendered":"<p>The suggestions of assylias (all Java versions) and Peter Lawrey (&gt;= Java5) are the standard way to go in this case.<\/p>\n<p>However I&#8217;d like to bring to your attention that preventing a extension of a static utility class is a very final decision that may come to haunt you later, when you find that you have related functionality in a different project and you&#8217;d in fact want to extend it.<\/p>\n<p>I suggest the following:<\/p>\n<pre><code>public abstract MyClass {\n\n    protected MyClass() {\n    }\n\n    abstract void noInstancesPlease();\n\n    void myMethod() {\n         ...\n    }\n    ... \/\/ More private methods and fields...\n\n}\n<\/code><\/pre>\n<p>This goes <em>against<\/em> established practice since it allows extension of the class when needed, it still prevents accidental instantiation (you can&#8217;t even create an anonymous subclass instance without getting a very clear compiler error).<\/p>\n<p>It always <em>pisses<\/em> me that the JDK&#8217;s utility classes (eg. java.util.Arrays) were in fact made final. If you want to have you own Arrays class with methods for lets say comparison, you can&#8217;t, you have to make a separate class. This will distribute functionality that (IMO) belongs together and should be available through <em>one<\/em> class. That leaves you either with wildly distributed utility methods, or you&#8217;d have to duplicate every one of the methods to your own class.<\/p>\n<p>I recommend to never make such utility classes final. The advantages do not outweight the disadvantages in my opinion.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The suggestions of assylias (all Java versions) and Peter Lawrey (&gt;= Java5) are the standard way to go in this case. However I&#8217;d like to bring to your attention that preventing a extension of a static utility class is a very final decision that may come to haunt you later, when you find that you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5027","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5027"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5027\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}