Question

I have just installed PMD to analyze my Java project. Really nice tool, highly recommended. Anyways, I got a few errors saying:

"An empty method in an abstract class should be abstract instead"

I checked out PMD documentation and the explanation says:

as developer may rely on this empty implementation rather than code the appropriate one

So I think I understand the reason behind this code style error, but consider the following scenario: I have an abstract class called Entity. This class has a boolean method with default implementation. (controls whether to delete its related entities upon deletion). Only a few of the derived classes override this default behavior to true.

Should I remove the default implementation and force all deriving classes to declare their behavior? Do you really think this pattern is such a bad practice?

Clarification: PMD treats a method with single return statement as empty.

Was it helpful?

Solution

I think it's just a guideline. It tells you so that you might want to reconsider your design, but if your design already makes perfect sense, there's no reason to obey a software instead of your brain.

OTHER TIPS

If you are on Java 1.8, you can make Entity an interface instead of an abstract class and write a default implementation for your method in it.

public interface Entity {
    default boolean yourMethod() {
        //default implementation ...
    }
}

You can use this for reference : https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top