9

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?

5 Answers 5

15

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

14

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.

4

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.

1

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.

1
  • I very much agree, Interfaces are what you want to give a clean name and include as a main part of your API. I think it's often better to use composition to fill the roles of Abstract classes, such as template methods etc. Abstract classes really should just be left in the realm of added convenience.
    – xtratic
    Commented Jun 19, 2020 at 14:38
-2

Another option to consider...

Abstract classes are considered "adapters". As far as naming, keep the file prefix grouped for easy visual identification. So for the op's example, consider using the following names.

public abstract class AjaxActionAdapter

public class AjaxActionBlue extends AjaxActionAdapter

public class AjaxActionCyan extends AjaxActionAdapter

Not the answer you're looking for? Browse other questions tagged or ask your own question.