Is it true that overriding concrete methods is a code smell? Because I think if you need to override concrete methods:
public class A{
public void a(){
}
}
public class B extends A{
@Override
public void a(){
}
}
it can be rewritten as
public interface A{
public void a();
}
public class ConcreteA implements A{
public void a();
}
public class B implements A{
public void a(){
}
}
and if B wants to reuse a() in A it can be rewritten as:
public class B implements A{
public ConcreteA concreteA;
public void a(){
concreteA.a();
}
}
which does not require inheritance to override the method, is that true?
Best Answer
No, it is not a code smell.
It lies within the responsabilities of each class to carefully consider if subclassing is appropriate, and which methods may be overridden.
The class may define itself or any method as final, or may place restrictions (visibility modifiers, available constructors) on how and where it is subclassed.
The usual case for overriding methods is a default implementation in the base class which may be customized or optimized in the subclass (especially in Java 8 with the advent of default methods in interfaces).
An alternative for overriding behaviour is the Strategy Pattern, where the behaviour is abstracted as an interface, and the implementation can be set in the class.