32

I'm using Checkstyle and am getting an error about this method:

public final String getAdmitCodeStatus() {
    return admitCodeStatus;
}

Here's the error I get:

Method 'getAdmitCodeStatus' is not designed for extension - needs to be abstract, final, or empty.

How is that method not compliant? Is there something I'm doing wrong that Checkstyle would bark at me about this method?

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
McGlone
  • 3,434
  • 5
  • 26
  • 31
  • Do you have another instance of `getAdmitCodeStatus` that would be in violation of that rule? For example, is there a base class that implements that method with a non-empty body? – C. K. Young Apr 25 '11 at 15:51
  • Can you make the class as final to see if it goes away (if your class can indeed be made final)? – CoolBeans Apr 25 '11 at 15:52
  • @Chris: Good idea, but I do not. I just did a search to make sure and I have no other methods that share this name. – McGlone Apr 25 '11 at 16:14
  • @CoolBeans: Unfortunately, I can not make this class either final or abstract. I do instantiate the class as is and, in some cases, extend it and instantiate the subclass. – McGlone Apr 25 '11 at 16:15
  • Did you get your question answered? – Snekse Jun 14 '12 at 15:40

3 Answers3

33

It looks to be caused by the DesignForExtension rule. According to the documentation:

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.

Source: http://sonar.15.n6.nabble.com/design-for-extension-rule-tp3200037p3200043.html

But since your method has a final modifier, I'd say you found a bug and might want to log a bug report. https://github.com/checkstyle/checkstyle/issues

Snekse
  • 15,474
  • 10
  • 62
  • 77
  • @Chris You are right. I've modified my answer to indicate that it doesn't really answer the OP's question. – Snekse Jan 17 '14 at 21:46
1

At first glance it looks like what on the earth kind of programming style is this... This just checks whether you are planing methods to be inherited or not...and then you can declare them final,abstract or empty implementation. Then you declare it final... ;) Either the class can be final or the methods individual depending on the requirement scenario.

Alind Billore
  • 696
  • 2
  • 10
  • 24
  • this doesn't seem to answer the question. the OP is declaring his method final, which would seem to satisfy the rule, he's looking for an explanation of why he still gets the error. – Nathan Hughes Oct 16 '13 at 14:19
  • It's My Bad Nathan ! I didn't read it properly. I am also new here so i beg your pardon and will take more care while posting from now. Thanks ! :) – Alind Billore Oct 16 '13 at 17:48
  • don't worry about it, misreading questions is very easy to do, I do it all the time. – Nathan Hughes Oct 16 '13 at 17:55
0

I think this check is useful and most of the times the warning is justified. Sometimes it is not appropriate and then I ignore it.

Jonathan Rosenne
  • 2,159
  • 17
  • 27