Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checkstyle Method is not designed for extension - needs to be abstract, final or empty [duplicate]

I have an interface

public interface LdapConnectionFactory {
    LdapConnectionWrapper getConnectionWrapper() throws LDAPException;
}

and the implementation class LdapConnectionFactoryImpl which implements the getConnectionWrapper() method.

public class LdapConnectionFactoryImpl implements LdapConnectionFactory {
    ...
    public LdapConnectionWrapper getConnectionWrapper() throws LDAPException {
        ...
    }
}

When I ran checkstyle, it flags the getConnectionWrapper() as error - Method 'getConnectionWrapper' is not designed for extension - needs to be abstract, final or empty. Any advice? Thanks.

like image 985
PassoGiau Avatar asked Jul 30 '13 12:07

PassoGiau


2 Answers

Checkstyle is saying that either the class or method should be marked as final since it is non-empty and therefore not designed for being overridden.

This is one of the rules of Checkstyle that I don't personally agree with and tend to disable.

like image 110
John B Avatar answered Oct 01 '22 02:10

John B


Checkstyle is throwing this message because you have provided an implementation but have not marked the method as final. You can disable this check via Eclipse Preferences and editing your Checkstyle configuration. The specific rule is Class Design > Design for Extension.

The description for the check is pretty decent, and is as follows:

Checks that classes are designed for extension. More specifically, it enforces a programming style where superclasses provide empty "hooks" that can be implemented by subclasses. The exact rule is that nonprivate, nonstatic methods of classes that can be subclassed must either be

  • abstract or
  • final or
  • have an empty implementation

Rationale: This API design style protects superclasses against beeing broken by subclasses. The downside is that subclasses are limited in their flexibility, in particular they cannot prevent execution of code in the superclass, but that also means that subclasses cannot corrupt the state of the superclass by forgetting to call the super method.

like image 31
Nizzo Avatar answered Oct 01 '22 01:10

Nizzo