chAmi
chAmi

Reputation: 1813

Avoid The overriding method merely calls the same method defined in a superclass Sonar violation in multiple inheritance

I have a class lets say BImpl which implement B interface and C abstract class. Both super classes have some method like doSomething() which implemented in abstract class C.

interface B
{
 Some doSomething();
}

abstract class C
{
 protected Some doSomething()
 {
   //Do something here...
 }
}

And i implement BImpl as follows:

class BImpl extends C implemensts B
{
  public Some doSomething()
  {
    super.doSomething()
  }
}

So i expose abstract class behavior with B type object. With this scenario i get PMD (in sonar) violation that say "The overriding method merely calls the same method defined in a superclass ". It quite not right to me because i expose other parent behavior. How can i avoid this?

Upvotes: 1

Views: 3409

Answers (4)

1) Looks like you have miss typed the implementation . It supposed to be like as below :

class BImpl extends C implements B
{
  public Some doSomething()
  {
    super.doSomething()
  }
}

because class cannot extend interface or implement abstract class.

2) as per my understanding about your question , the implementation of doSomething() in BImpl is redundant.

Class BImpl extends C implements B {

}

is logically equivalent to above implementation. That is the reason SONAR is giving violation. Well, if doSomething() in BImpl has different logic than in abstract class. I don`t think SONAR will give any violation as there will be no redundant logic.

Upvotes: 2

Mithfindel
Mithfindel

Reputation: 4724

It's a known limitation of this PMD rule and of the related Squid (SonarQube internal) rule. Feel free to vote for the resolution of this issue in next version of the Java plugin.

Upvotes: 2

Gauthier JACQUES
Gauthier JACQUES

Reputation: 675

If you do not agree with some Sonar rules, which is not wrong, some rules in the default profile are more than arguable, just disable from the profile you're using to analyze your project and the violations will disappear after next analyze.

Upvotes: 1

Attila Neparáczki
Attila Neparáczki

Reputation: 476

just an idea, typecast?

public Some doSomething(){
  ((B)this).doSomething()
}

Upvotes: 0

Related Questions