Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have an abstract class that will have many implementors. There are, of course, many places to put abstract classes in a Java package structure. Should the abstract class be in the same package as the implementors, or should it be in an "abstract package" package, or somewhere else? Please explain why you would place it there - I'm looking for design discussion, not just what I should do in this particular case.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

I would put it (like any other class) in the package it belongs most from it's content. That mostly - but not everytime - means, that you put it in the same package as implementing classes. Sometimes you put an abstract class in the same package as some other class using this abstract class - it's part of the API of the other class. Implementing classes could be somewhere else in that case. I never use extra packages for abstract classes or other stuff like exception-classes or so.

Why so? I prefer to use package-structure as building a system for categorizing the content of the software, instead as using it for implementation details. The abstract-class may become later a valid non-abstract-class without breaking the API. But if I have to move it's package it would break again old code. Unnessecary I think.

share|improve this answer

I was just wondering about the same thing (but for interface classes). So lets add a post to this discussion.

I have a package com.cie.devices where I put devices which I control using Java software. Now comes the time where there are enough devices to start building a hierarchy of devices. I was wondering if I should put my TypeA interface for type A devices in com.cie.devices.typea, together with implementing classes. My main argument for not doing so was that putting interfaces in com.cie.devices made it easy to quickly identify all types of devices by looking at the interfaces in that package. My main argument for doing so is that it brings all type A related classes together.

In the end, I guess I'll all type related devices in the same package (i.e. all type A in com.cie.devices.typea) because 1) you already quickly see all types of devices looking at the subpackages of com.cie.devices, and 2) it unclutters the com.cie.devices packages which will probably still contain some lonely classes which do not have enough friends to be pushed a level lower in the class hierarchy.

So that's it. This is another case with another reasoning behind it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["                  <li><a href=\"//area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"//careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2015 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc by-sa 3.0</a> \r\n                with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2015.6.30.2690\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Programmers Stack Exchange works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div"], "copyright__copyright__copyright": [" of com.cie.devices, and 2) it unclutters the com.cie.devices packages which will probably still contain some lonely classes which do not have enough friends to be pushed a level lower in the class hierarchy.\n\nSo that's it. This is another case with another reasoning behind it.\n\nshare|improve this answer\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}