Is it considered better form for an abstract class to include abstract methods from an interface?

107 views Asked by At

I realize the declaration of abstract methods left over from an interface is optional, but I'm wondering whether or not it's considered better form semantically to include them.

Interface:

public interface Interface {
//Other methods....
public void unimplementedMethod();
}

Abstract class:

public abstract Class AbstractClass implements Interface {
//Implemented methods....
public abstract void unimplementedMethod();   //Should include or better to leave out?
}
1

There are 1 answers

0
nrainer On BEST ANSWER

You should leave them out. They don't add any benefit and just increase the amount of code to be maintained. If you want to remove the method you will need to do that in one more location.

Code quality analyzers generally demand the reduction of anything which is not needed: redundant modifiers (eg: public abstract in interfaces), declared runtime exceptions (throws IllegalArgumentException), redundant throws (throws Exception, IOException) and so on.

However, you could consider implementing the default behaviour of methods in the abstract class (event listener methods with an empty body, configuration property methods which return the default value, ...). Eg:

public boolean isPersistent() {
  return true;
}

isPersistent() only needs to be overwritten in sub classes which use another value than the default one.