11
As a (pedantic) beginner Java programmer I would like to know, is it a good practice to move a common block of code that all subclasses use to a separate protected (final) method in parent class? Tasks like filling lists with common values, or common filtering algorithms, etc... Is it good to also use protected static methods?
class A {
protected final List<String> getVariants() {...}
protected final List<String> filterResults(List<String> variants) {...}
}
class B extends A {
public List<String> doSomethingUsefull() {
List<String> commonVariants = getVariants();
...
return filterResults(commonVariants);
}
}
class C extends A {
public void doSomethingUsefull() {
List<String> commonVariants = getVariants();
...
return filterResults(commonVariants);
}
public void doMoreUsefullThings() {
List<String> commonVariants = getVariants();
...
return filterResults(commonVariants);
}
}