Java abstract class naming

javanaming

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?

Best Answer

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

Related Topic