You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] AbstractClassWithoutAnyMethod: False positive for empty subclasses that inherit methods #1237
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().");
}
}
@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?
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.
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:
publicabstractclassViewextendsApplication{
//Contains especially properties extending all my viewsprotectedabstractvoidstartImpl(Stagestage);
@Overridepublicfinalvoidstart(Stagestage){
//do some stuff all views have in common (like some resources, icons, certain behaviour,...)startImpl(stage);
//do further stuff
}
}
publicinterfaceLogin {
// marker interface
}
publicclassSshLoginextendsViewimplementsLogin {
@OverridepublicvoidstartImpl() {
//do additional stuff for the view used when logging in using SSH.
}
}
publicclassDefaultLoginextendsViewimplementsLogin {
@OverridepublicvoidstartImpl(){
//do additional stuff for the view when NOT logging in using SSH.
}
}
// wherever you need a login screen:public <TextendsView & Login> voidshowNextView(TnextView) {
....
}
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.
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.
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.
changed the title [-][java] AbstractClassWithoutAnyMethod - Only detecting empty classes[/-][+][java] AbstractClassWithoutAnyMethod: False positive for empty sub classes that inherit methods[/+]on Aug 20, 2026
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
Activity
jsotuyod commentedon Jul 14, 2018
@TrackerSB I'm having trouble understanding the issue.
The rule is meant to do exactly that, as per the description:
I understand under your scenario, flagging
EmptySubclassis weird (as it's clearly not a data container), although having it add no value overAbstractSuperclassseems fishy. But I don't think this is what you are considering to be a problem here... could you further explain yourself?TrackerSB commentedon Jul 14, 2018
When using the names for the classes used in my project it may get clearer. It is a JavaFX project.
You have (imports omitted):
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.jsotuyod commentedon Jul 14, 2018
Ok, I see.
The flagging is definitely misleading, although the code is still somewhat fishy. You are basically using
Loginas 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:
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?
TrackerSB commentedon Jul 14, 2018
I think using a marker interface is a good way to go. I agree with your suggestions.
UncleOwen commentedon Aug 13, 2026
I disagree with @jsotuyod that "The rule is meant to do exactly that".
EmptySubclassis 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?
zbynek commentedon Aug 13, 2026
@UncleOwen I agree.
OTOH I see @jsotuyod 's point that classes like
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.
[-][java] AbstractClassWithoutAnyMethod - Only detecting empty classes[/-][+][java] AbstractClassWithoutAnyMethod: False positive for empty sub classes that inherit methods[/+][-][java] AbstractClassWithoutAnyMethod: False positive for empty sub classes that inherit methods[/-][+][java] AbstractClassWithoutAnyMethod: False positive for empty subclasses that inherit methods[/+]