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.

Example:

You create an abstract class for Ajax actions. Different Ajax actions will all extend this class.

Problem:

  1. Do you name the class AjaxAction or AbstractAjaxAction?
  2. When you create subclasses, do you name them:
    • Action
    • AjaxAction
    • Just name them what they are

I have not been able to find what the "official" convention is. I don't see a need for the "Abstract" prefix or any required naming convention in the subclasses. (It may fit anyway, depending.)

Is there an official convention? Could you link me to it? And what are your opinions on this subject?

share|improve this question

4 Answers 4

up vote 12 down vote accepted

When possible, I try to leave implementation details out of names, including a prefix/suffix for an abstract class or interface.

One place to look for guidance/inspiration on naming is the Java API. I can't think of any parts that use the prefix (with a huge exception being the Collections API, which uses the Abstract prefix heavily). The following search will yield some pretty good examples:

"public abstract class" at download.oracle.com/javase/6

You'll find abstract classes are named something abstract and concrete subclasses are named something more concrete. For example, abstract class Reader has concrete subclasses BufferedReader, StringReader, InputStreamReader, etc.

Since your concrete subclasses are just more specific versions of your abstract class, it is intent revealing to choose your names in the same way. In your example, I would name the abstract class AjaxAction and use that in the names for the concrete subclasses (SpecificAjaxAction).

share|improve this answer

Choose something, and be consistent. Whether you use the suffix Base to indicate an intended base class, or the prefix Abstract, just be consistent about it.

There are as many opinions on this kind of thing as there are, well, programmers. My opinion, for what it's worth, as that consistency in naming, within a project (or development group or whatever) is more important than any specific naming scheme.

share|improve this answer

I would name the abstract class AjaxActionBase and suffix child classes with either 'Action' or 'AjaxAction', whichever ended up more naturally readable for the actions I needed to create.

share|improve this answer

I prefer to put a hint that a class is abstract in the name for one simple but very important reason:

  • It's better to build your APIs around interfaces rather than abstract classes.
  • You want to use the simple name e.g. "Action" to name your interface. See for example List or Map in the Java Collections API - all of these are interfaces.

Hence I would typically call a top-level abstract class either AbstractAction or BaseAction. Unless there is a very specific need, I would normally not make these public, so that users of the code are steered into using the interface. Abstract base classes should usually be an implementation detail rather than a feature of your API, unless you are very very careful about how you want people to extend them.

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.