Search code examples
oopinterfaceconventions

Name for the methods of a class which are specifically theirs


I have a class which implements several interfaces. I have seen in other places comments being used to indicate which block of methods belong to which interface, such as:

public class MagicalAnimal implements InterfaceBark,
                                      InterfaceFly {
    // InterfaceBark
    public void barkQuietly() { }
    public void barkLoudly() { }

    // InterfaceFly
    public void moveWings() { }
    public void land() { }

    // ???
    public void shootLasers() {  }
    public void teleport() {  }
}

What would be the comment I should put before listing the methods which are not implemented from any interface but are especific to MyClass (indicated in the code as // ???)?


Solution

  • Put whatever you want. I recommend putting nothing.

    The big concern here is obsoletion of comments, say you put:

    // Other
    

    And then you decide to have an interface

    public interface MagicPowers {
      public void shootLasers();
      public void teleport();
    }
    

    Now, you have to remember to change the comment (which must be done manually) in addition to adding the interface (which can be done automatically by the IDE).

    Most IDEs have the ability to easily see what interface a comment comes from; these comments are not necessary.