Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Interface, AbstractClass and Enum naming convention

We are having this discussion in our team about code conventions for Java for:

  • interface: Foo or IFoo or FooInterface?

  • abstract: Foo or AbstractFoo?

  • Enums: Foo or FooEnum?

I'm basically trying to put my personal preferences aside :) so reasons to back up one or other convention are very welcome.

like image 447
roundcrisis Avatar asked Jun 17 '09 10:06

roundcrisis


People also ask

What is the naming convention for interface in Java?

Interface names should be capitalized like class names. Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter.

What are the two naming conventions in Java?

Using the right letter case is the key to following a naming convention: Lowercase is where all the letters in a word are written without any capitalization (e.g., while, if, mypackage). Uppercase is where all the letters in a word are written in capitals.

What are the three Java naming convention?

CamelCase in Java naming conventions Java follows camel-case syntax for naming the class, interface, method, and variable.

What the naming convention should be for variables in Java?

For variables, the Java naming convention is to always start with a lowercase letter and then capitalize the first letter of every subsequent word. Variables in Java are not allowed to contain white space, so variables made from compound words are to be written with a lower camel case syntax.


2 Answers

In Java: Foo, AbstractFoo and Foo - although AbstractFoo could just be Foo.

Evidence:

  • java.util.List (interface)
  • java.util.AbstractList (abstract class)
  • java.util.Formatter.BigDecimalLayoutForm (enum)

For the interface part, see the Naming Conventions section of the Java Coding Conventions document. It doesn't talk about enums and abstract classes though.

like image 117
Jon Skeet Avatar answered Sep 21 '22 17:09

Jon Skeet


From my blog:

  • Foo - The interface ultimately defines the concept, so it should have the best name.
  • AbstractFoo - An abstract implementation intended to be used as the base of a hierarchy of classes.
  • BaseFoo - An implementation intended to be used as the base of a hierarchy of classes, where the base class could be used on its own if necessary.
  • DefaultFoo - A "default" implementation that would be appropriate for the majority of typical use cases.
  • SimpleFoo - A "simple" implementation with no unexpected functionality, perhaps as an example or as a mock. A simple POJO would be a good "simple" implementation.
  • {Descriptive}Foo - Other implementations should describe what makes them unique.

The blog also discusses reasons against some of the other names.

like image 44
JodaStephen Avatar answered Sep 22 '22 17:09

JodaStephen