Skip to content

[java] AbstractClassWithoutAnyMethod: False positive for empty subclasses that inherit methods #1237

Closed

Description

@TrackerSB

Affects PMD Version: 6.4.0
Running PMD through: Maven
Maven PMD plugin version: 3.10.0

Rule: AbstractClassWithoutAnyMethod

Description: When a class extends an abstract class (e.g. to introduce a further layer of abstraction) and it does neither implement the abstract methods of its superclass nor declare further ones it is only detected as AbstractClassWithoutAnyMethod if it is empty. When defining any arbitrary non-abstract method it is not detected as such.

Code Sample demonstrating the issue:
AbstractSuperclass.java

public abstract class AbstractSuperclass {
    protected abstract void foo();
}

EmptySubclass.java

public abstract class EmptySubclass extends AbstractSuperclass {
}

NonEmptySubclass.java

public abstract class NonEmptySubclass extends AbstractSuperclass {
    public void bar() {
        System.out.println("Called bar().");
    }
}

Activity

  1. jsotuyod commented on Jul 14, 2018

    @jsotuyod
    Member

    @TrackerSB I'm having trouble understanding the issue.

    The rule is meant to do exactly that, as per the description:

    If an abstract class does not provides any methods, it may be acting as a simple data container that is not meant to be instantiated. In this case, it is probably better to use a private or protected constructor in order to prevent instantiation than make the class misleadingly abstract.

    I understand under your scenario, flagging EmptySubclass is weird (as it's clearly not a data container), although having it add no value over AbstractSuperclass seems fishy. But I don't think this is what you are considering to be a problem here... could you further explain yourself?

  2. TrackerSB commented on Jul 14, 2018

    @TrackerSB
    Author

    When using the names for the classes used in my project it may get clearer. It is a JavaFX project.
    You have (imports omitted):

    public abstract class View extends Application{
        //Contains especially properties extending all my views
    
        protected abstract void startImpl(Stage stage);
    
        @Override
        public final void start(Stage stage){
            //do some stuff all views have in common (like some resources, icons, certain behaviour,...)
            startImpl(stage);
            //do further stuff
        }
    }
    
    public abstract class Login extends View {
        //This one is needed to make sure at certain points in the program I can only pass Login-Views and no other views.
        //Currently it has no other use.
    }
    
    public class SshLogin extends Login {
        @Override
        public void startImpl(){
            //do additional stuff for the view used when logging in using SSH.
        }
    }
    
    public class DefaultLogin extends Login {
        @Override
        public void startImpl(){
            //do additional stuff for the view when NOT logging in using SSH.
        }
    }
    

    Here the Login class is marked as AbstractClassWithoutAnyMethod. Since it does not override startImpl(Stage) it has to be abstract. So it is not abstract "to prevent instantiation" and it is in my opinion also not "misleadingly abstract" since it needs to abstract by inheritance and not implementing the abstract methods of the superclass. And there is also no need for a protected constructor since it has no properties to be initialized.
    And additionally I recognized when I define any arbitrary method (does not need to be abstract) it not marked as AbstractClassWithoutAnyMethod.

  3. jsotuyod commented on Jul 14, 2018

    @jsotuyod
    Member

    Ok, I see.

    The flagging is definitely misleading, although the code is still somewhat fishy. You are basically using Login as a marker, although it's not defining any login-specific behavior. This is normally done with marker interfaces (ie: Cloneable, Serializable, etc.). The use of interfaces is preferred due to the extend one, implement many rule.

    Having no behavior for login views is just a developer notice for "handle login somewhere here".

    I'd probably just use a marker interface:

    public abstract class View extends Application{
        //Contains especially properties extending all my views
    
        protected abstract void startImpl(Stage stage);
    
        @Override
        public final void start(Stage stage){
            //do some stuff all views have in common (like some resources, icons, certain behaviour,...)
            startImpl(stage);
            //do further stuff
        }
    }
    
    public interface Login {
        // marker interface
    }
    
    public class SshLogin extends View implements Login {
        @Override
        public void startImpl() {
            //do additional stuff for the view used when logging in using SSH.
        }
    }
    
    public class DefaultLogin extends View implements Login {
        @Override
        public void startImpl(){
            //do additional stuff for the view when NOT logging in using SSH.
        }
    }
    
    // wherever you need a login screen:
    public <T extends View & Login> void showNextView(T nextView) {
      ....
    }

    I wouldn't change the rule (the name is still accurate, we have an abstract class not defining any methods), but we should update the documentation and rule description for this scenario to be clearer for users.

    Would you disagree with this approach?

  4. TrackerSB commented on Jul 14, 2018

    @TrackerSB
    Author

    I think using a marker interface is a good way to go. I agree with your suggestions.

  5. added
    in:documentation
    on Jul 14, 2018
  6. UncleOwen commented on Aug 13, 2026

    @UncleOwen
    Member

    I disagree with @jsotuyod that "The rule is meant to do exactly that". EmptySubclass is clearly not a "simple data container", that's why I think it should not trigger this rule. That's why I'm inclined to merge #6972.

    @pmd/maintainers Does anyone disagree?

  7. zbynek commented on Aug 13, 2026

    @zbynek
    Contributor

    @UncleOwen I agree.

    OTOH I see @jsotuyod 's point that classes like

    public abstract class EmptySubclass extends AbstractSuperclass {
    }
    

    suggest there's something wrong with the design, but I think flagging them is outside of the scope of this rule. Maybe a new rule could be added that flags "extending class without value added", such rule should not be restricted to abstract classes though.

  8. added this to the 7.27.0 milestone on Aug 15, 2026
  9. added
    a:false-negative
    a:false-positive
    and removed
    in:documentation
    a:false-negative
    on Aug 20, 2026
  10. changed the title [-][java] AbstractClassWithoutAnyMethod - Only detecting empty classes[/-] [+][java] AbstractClassWithoutAnyMethod: False positive for empty sub classes that inherit methods[/+] on Aug 20, 2026
  11. changed the title [-][java] AbstractClassWithoutAnyMethod: False positive for empty sub classes that inherit methods[/-] [+][java] AbstractClassWithoutAnyMethod: False positive for empty subclasses that inherit methods[/+] on Aug 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions